| | |
getline failing to read from data file?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
sort of a noob as i have been doing c++ for only a couple months now. my problem is that i am using a function to pass data from a file into an array then into a .out file. the function im using to read the data from the file is using a while loop. the data file consists of the name of a store, then the pattern of a product code then product name then product price repeated over and over for multiple products. the product name has embedded blanks which i try to read with getline but fail horribly. no compiler error just debug assertion failure. using vs. can provide more info if needed. dont think you need main or other functions.function code for reading data is as follows:
C++ Syntax (Toggle Plain Text)
long readFile (string code[], string product[], double price[]) { string temp, store; long count; ifstream fin ("program5.txt"); getline(fin,store); count=0; fin>>temp; while (!fin.eof()) { code[count]=temp; getline(fin,product[count]); fin>>price[count]; count++; fin>>temp; } fin.close(); return count; }
Last edited by Ancient Dragon; Apr 13th, 2008 at 10:01 pm. Reason: replace icode tags with code tags
Here is how to code that loop. You can delete lines 8, 10 and 19
C++ Syntax (Toggle Plain Text)
int count = 0; while( getline(fin, product[count]) ) count++;
Last edited by Ancient Dragon; Apr 13th, 2008 at 10:09 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
There are two versions of getline() -- cin.getline() is for character arrays and getline(cin,...) if for std::strings.
You didn't say how the data file is formatted, so I just assumed it has all the information on one line because you said you wanted to use getline(). Post a few lines of the data file so that we can see how it should be read.
You didn't say how the data file is formatted, so I just assumed it has all the information on one line because you said you wanted to use getline(). Post a few lines of the data file so that we can see how it should be read.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
First I would create a structure (or class if you like) to contain all the information for one company. Change the function to accept a reference to a vector of structures instead of all those string arrays. That will simplify your intire program.
C++ Syntax (Toggle Plain Text)
struct company { string companyName; string something; // e.g. A123, whatever that represents string somethingElse // e.g. case // etc. etc for each of the other fields }; // create an array to hold the structures long readFile (vector<company>& colist) { // now read the data struct company co; while( getline(fin,co.company) ) { getline(fin,co.something); getline(fin,co.somethingElse); // etc for each field // // finally put co into the vector colist.push_back(co); } }
Last edited by Ancient Dragon; Apr 14th, 2008 at 2:13 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 0
ok since im a beginner this was explained to me in a different way that was easier to understand. not bashing anyone else's solution, but this fix requires only one line of code. im just posting this in case someone else has a similar problem. apparently after reading the product code (the A123) a new line was trying to be read by the getline. therefore the only thing needed was:
right before the getline trying to read the product name. a simple fix.
C++ Syntax (Toggle Plain Text)
fin.ignore(80, '\n');
right before the getline trying to read the product name. a simple fix.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Connect 4 computer player
- Next Thread: Windows API - Use QT Now?
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






