Reading a text file / carriage return

S

stephane

Hi,

I don't know how to test something.

I read at file.txt caracter by caracter

And I would like to know when it is the end of my line in the text file.
exemple of a line:

stephane;vollet;1234;

what could I put into the bracket to test the end of the line?

while(!=' carriage return ')
{

I don't if that clear enough...

thanks for your time anyway.

stephane
 
R

Rolf Magnus

stephane said:
Hi,

I don't know how to test something.

I read at file.txt caracter by caracter

And I would like to know when it is the end of my line in the text file.
exemple of a line:

stephane;vollet;1234;

what could I put into the bracket to test the end of the line?

while(!=' carriage return ')
{

char c;
//...
while (c != '\n')
{
//....
}

Btw: is there a reason why you don't read in the whole line at once?
 
S

stephane

Yeah I don't read the whole line because I need to get the name and surname
separated by " ; " and put them in a binary file in a struct...

You see?
 
R

Rolf Magnus

stephane said:
Yeah I don't read the whole line because I need to get the name and
surname separated by " ; " and put them in a binary file in a struct...

You see?

Then try something like that:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
std::ifstream file("file.txt");
std::string line;
while (std::getline(file, line))
{
std::string name, surname, rest;
std::istringstream stream(line);

std::getline(stream, name, ';');
std::getline(stream, surname, ';');
std::getline(stream, rest);

if (!stream)
std::cout << "Corrupt line found\n";
else
std::cout << "Name: " << name
<< ", Surname: " << surname
<< ", rest of the line: " << rest << '\n';
}
}
 
M

Matt Wharton

what could I put into the bracket to test the end of the line?
while(!=' carriage return ')
{

Depends on how your platform represents end of line (usually either '\n' or
"\r\n").

Something like:

while ( ch != '\n' )

is probably the kind of thing you're looking for.

HTH,

-Matt
 
R

Rolf Magnus

Matt said:
Depends on how your platform represents end of line (usually either '\n'
or "\r\n").

Not if the file was opened in text mode. Then, the line ending is always
automatically converted from the platform's native format to '\n'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top