| | |
Reading multiple lines from a txt file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
ok here is an example of the type txt file I am trying to read
12345678901234567890123456789012345678901234567890
Bugs Bunny Jr. 1234 1001.01
Dr. Wiley Coyote 2345 1002.02
Taco Speedy Gonzales3456 1003.03
Billy the Goat 4567 1004.04
Porky Pig 5678 1005.05
what I am trying to do is skip the first line and then read the rest storing it in a linked list of structs the problem I am running into is that it will get all the way to the third line then skip the fourth read the fifth and get junk for the sixth.
here is the read function I am using all variables are global
void readfilefn()
{
firstcustomer=new customerType;
fin.ignore(50,'\n');
fin.get(firstcustomer->name, 21);
currcustomer=firstcustomer;
fin>>currcustomer->pin;
fin>>currcustomer->balance;
firstcustomer->link=currcustomer;
//check if the file is being read properly
cout<<"\nCustomer Name: "<<firstcustomer->name<<endl;
cout<<"Pin : "<<firstcustomer->pin<<endl;
cout<<"Balance : "<<firstcustomer->balance<<endl;
counter++;
while(!fin.eof()&&fin.peek()=='\n')
{
newcustomer=new customerType;
int row=0;
while(row<counter)
{
fin.ignore(50,'\n');
row++;
}
fin.get(newcustomer->name, 21);
fin>>newcustomer->pin;
fin>>newcustomer->balance;
currcustomer->link=newcustomer;
currcustomer=newcustomer;
cout<<"\nCustomer Name: "<<currcustomer->name<<endl;
cout<<"Pin : "<<currcustomer->pin<<endl;
cout<<"Balance : "<<currcustomer->balance<<endl;
//if(fin.peek()!='\n')fin.ignore();
counter++;
}//end of while
cout<<"There are "<<counter<<" accounts"<<endl;
}
12345678901234567890123456789012345678901234567890
Bugs Bunny Jr. 1234 1001.01
Dr. Wiley Coyote 2345 1002.02
Taco Speedy Gonzales3456 1003.03
Billy the Goat 4567 1004.04
Porky Pig 5678 1005.05
what I am trying to do is skip the first line and then read the rest storing it in a linked list of structs the problem I am running into is that it will get all the way to the third line then skip the fourth read the fifth and get junk for the sixth.
here is the read function I am using all variables are global
void readfilefn()
{
firstcustomer=new customerType;
fin.ignore(50,'\n');
fin.get(firstcustomer->name, 21);
currcustomer=firstcustomer;
fin>>currcustomer->pin;
fin>>currcustomer->balance;
firstcustomer->link=currcustomer;
//check if the file is being read properly
cout<<"\nCustomer Name: "<<firstcustomer->name<<endl;
cout<<"Pin : "<<firstcustomer->pin<<endl;
cout<<"Balance : "<<firstcustomer->balance<<endl;
counter++;
while(!fin.eof()&&fin.peek()=='\n')
{
newcustomer=new customerType;
int row=0;
while(row<counter)
{
fin.ignore(50,'\n');
row++;
}
fin.get(newcustomer->name, 21);
fin>>newcustomer->pin;
fin>>newcustomer->balance;
currcustomer->link=newcustomer;
currcustomer=newcustomer;
cout<<"\nCustomer Name: "<<currcustomer->name<<endl;
cout<<"Pin : "<<currcustomer->pin<<endl;
cout<<"Balance : "<<currcustomer->balance<<endl;
//if(fin.peek()!='\n')fin.ignore();
counter++;
}//end of while
cout<<"There are "<<counter<<" accounts"<<endl;
}
Hi mate,
I didn't read through all the source code, but basically you can use getline to do this.
getline has the following parameters:
std::getline ( source stream , desination stream , delimeter )
There are several ways to use this..
e.g.
or something like this:
I shouldn't be giving away the solution so cheaply, but it will be something like this:
I didn't read through all the source code, but basically you can use getline to do this.
getline has the following parameters:
std::getline ( source stream , desination stream , delimeter )
There are several ways to use this..
e.g.
C++ Syntax (Toggle Plain Text)
... std::string temp, total; while (!file.eof()) { std::getline(file,temp); // delimeter is the newline char by default total += temp; }
or something like this:
C++ Syntax (Toggle Plain Text)
... while(std::getline(file,temp)) str += temp;
I shouldn't be giving away the solution so cheaply, but it will be something like this:
C++ Syntax (Toggle Plain Text)
getline(file,str); for (int counter = 0; !file.eof(); counter++) { getline(file,someKindOfString); // do whatever else with string } cout << counter << "accounts";
Last edited by phalaris_trip; Jul 11th, 2007 at 6:55 pm.
Of the 3 solutions you posted, this is the only one which won't crash and burn once the file hits EOF. the EOF flag is not set until after the stream reaches EOF (A read is attempted and fails) - which means your final call to getline will break, causing problems for the final iteration of the loop.
The loop in this example doesn't rely on EOF, so it works well.
As a general rule of thumb, you should never use EOF as a loop's terminating condition when reading from a file.
The loop in this example doesn't rely on EOF, so it works well.
As a general rule of thumb, you should never use EOF as a loop's terminating condition when reading from a file.
¿umop apisdn upside down? The problem probably is at Speed Gonzales3456. There is no space between es and 3456, so afterwards everything is out of sync by one field.
You last post wasn't visible to me until I posted this one
You last post wasn't visible to me until I posted this one
Last edited by Tight_Coder_Ex; Jul 11th, 2007 at 9:45 pm. Reason: Other post showed up within seconds of mine
![]() |
Similar Threads
- Reading txt file into Hash Table (C++)
- Setting an array and reading in a txt file backwards (C++)
- how to move to the second line in C++ .txt file reading? (C++)
- creating Contact form to email and txt file (PHP)
- reading txt file into array (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- uploading .txt file via phpmyadmin to Mysql (MySQL)
Other Threads in the C++ Forum
- Previous Thread: What are vectors?
- Next Thread: How to move a selected item up/down in List Box?
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





