LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2008, 02:30 PM   #1
valembe
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Rep: Reputation: 0
String to Char (c++)


Code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	ifstream inFile;
	string line;
	char f[1024];
	char *pt;

	inFile.open("live.txt");
    if (!inFile)
	{
	    cout << "Unable to open file";
        exit(1); // terminate with error
    }
	
	while(getline( inFile, line ))
	{
                
		strncpy(f,line,);
		pt=strtok(f,",");
		
               // .....do some analysis on the token		
	}

	inFile.close();
		
	return 0;
}

In my code above im reading a line from a file then i want to tokenize the string and analyse the contents individually. strtok cant take the string "line" and need you help on how i can convert the string "line" to char "f" array...Please correct me where im wrong

Last edited by valembe; 10-22-2008 at 02:33 PM.
 
Old 10-22-2008, 03:49 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
The c_str() member of string might work well for this, but the find() member might be better, IMO (then you might not need the conversion).
 
Old 10-22-2008, 07:04 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
BTW - it looks like you're using Microsoft Visual Studio ("stdafx.h", "_tmain()", "TCHAR *", etc).

Be advised that newer versions of Visual Studio default to 16-bit "#define UNICODE", so they're particularly hostile to ASCII-oriented functions like "getline ()" and "strtok()" unless you carefully cast everything in sight...

Last edited by paulsm4; 10-22-2008 at 07:06 PM.
 
Old 10-23-2008, 11:51 AM   #4
valembe
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
BTW - it looks like you're using Microsoft Visual Studio ("stdafx.h", "_tmain()", "TCHAR *", etc).

Be advised that newer versions of Visual Studio default to 16-bit "#define UNICODE", so they're particularly hostile to ASCII-oriented functions like "getline ()" and "strtok()" unless you carefully cast everything in sight...


Thank you guys for your valued advise!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ : replacing a char in a string cb951303 Programming 20 02-25-2015 08:18 PM
get first char of a string? raskol Programming 2 04-05-2008 06:11 PM
[] string.spits(char) in C++ manolakis Programming 2 09-24-2007 09:05 AM
How to convert string to char? twirl Programming 27 10-13-2005 07:11 AM
Convert C++ string to C char* nyk Programming 3 06-17-2004 08:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration