•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,700 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,172 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 5483 | Replies: 4
![]() |
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Rep Power: 2
Solved Threads: 0
I have a s1.txt like this :
3,R,G,T
1,E
9,T,U,Y,O
Now I want to read each character of a each line ...do some analysis and then read each character of the next line ...do some analysis ...so on till EOF.
How do I go abt this?
Well ...I jus wrote a simple program to read a file as below:
Doing the above ...I get the entire file as output ... why is'nt it stopping execution as soon as it hits a newline as I mentioned in the first while loop? After I get the first line ...how do I parse through it to analyse each character in the line?
3,R,G,T
1,E
9,T,U,Y,O
Now I want to read each character of a each line ...do some analysis and then read each character of the next line ...do some analysis ...so on till EOF.
How do I go abt this?
Well ...I jus wrote a simple program to read a file as below:
c Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { //char sum = 0; char x; ifstream inFile("s1.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); } while (x != '\n') { while (inFile >> x) { cout<<"x = "<<x<<endl; } } inFile.close(); getchar(); return 0; }
Last edited by WaltP : Apr 12th, 2007 at 2:34 pm. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
•
•
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation:
Rep Power: 4
Solved Threads: 50
You know there is also www.google.com where you can search for words like "C++ split line"
or "C++ tokens"...
Or use RWCTokenizer..
or "C++ tokens"...
Or use RWCTokenizer..
•
•
Join Date: Dec 2006
Location: india
Posts: 1,045
Reputation:
Rep Power: 9
Solved Threads: 157
a. read one line
b. strip off unwanted chars (non-printable/whitespace/,)
c. analyze the remaining chars.
repeat a,b,c till end of file.
b. strip off unwanted chars (non-printable/whitespace/,)
c. analyze the remaining chars.
repeat a,b,c till end of file.
struct is_2b_thrown_away
{
bool operator() ( char ch ) const
{ return !isprint(ch) || isspace(ch) || (ch==',') ; }
};
void process_file( const char* file_name )
{
ifstream file(file_name) ;
string line ;
while( getline( file, line ) )
{
vector<char> chars ;
remove_copy_if( line.begin(), line.end(), back_inserter(chars),
is_2b_thrown_away() ) ;
// analyse characters in vector chars
}
}
Mostly what vijayan121 suggests
a. read one line
b. analyze each character
c. do what you need with each character
repeat a,b,c till end of file.
a. read one line
b. analyze each character
c. do what you need with each character
repeat a,b,c till end of file.
string line ;
while( getline( file, line ) )
{
for (i=0; i < line.length(); i++)
{
if (line[i] ...) // look at each character and process it accordingly
}
} Age is unimportant -- except in cheese
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- C++ Reading from a text file (C++)
- Reading a text file (C++)
- Reading in a text file string is not complete (Pascal and Delphi)
- how do i read the last line of a text file? (Python)
Other Threads in the C++ Forum
- Previous Thread: Working with classes
- Next Thread: Borland C++



Linear Mode