Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have written a C++ program I have multiple of CSV file used as input, which I open one at a time and close it after extracting data to a output file which is the only file.

I run getline(inFile,line);
outFile << line << endl;

I run this code, and only part of it is goes to the output file I got, also have spacing randomly to specific file and inconsistent

But when I slower the code, like system("Pause") in the loop, I can get extract what I want perfectly....

Is my program running to fast, why getline would be skipping part of what things I want?

I really have no idea where the problem is coming from, or where to start

Many Thanks!

OP's code:
C++
if (dataname[i] == dataname)
{
    inFile.seekg(datalength[i], ios::beg);
    for (int j = 0; j < datacount[i]; j++)
    {
        getline(inFile, line);
        outFile << line << endl;
    }

}
Posted
Updated 6-Jun-13 0:37am
v3
Comments
shahrilhafiz 5-Jun-13 1:52am    
Did you "Pause" each of the getline? and get the correct data while fully running it cause the data not correct?
Member 10093180 6-Jun-13 5:27am    
Yes this is correct
Richard MacCutchan 5-Jun-13 4:19am    
Show your code, there must be an error somewhere, and using "Pause" to get round it is not a solution.
Member 10093180 6-Jun-13 5:28am    
if (dataname[i] == dataname)
{
inFile.seekg(datalength[i], ios::beg);
for (int j = 0; j < datacount[i]; j++)
{
getline(inFile, line);
outFile << line << endl;
}

}

Many Thanks Richard!!
Richard MacCutchan 6-Jun-13 5:47am    
if (dataname[i] == dataname)
What do you expect the result of that statement to be?

seekg() is generally used on streams opened in binary mode. getline() is generally used on streams opened in non-binary (aka text) mode.

Either way, using them together can give results like you describe. The solution: don't use them both on the same stream.
 
Share this answer
 
Comments
Richard MacCutchan 6-Jun-13 7:32am    
This is not true. I suspect that having removed the seekg calls from your program, you also removed the code containing the bug(s).
Flush the outfile periodically. Sometimes data doesn't get written properly to the underlying medium.

Call the function after every write operation
C++
outFile.flush();


Good Luck!
 
Share this answer
 
C++
open the output file
while (there are more input files)
{
    open the next input file
    while (there is more data in the input file)
    {
        read the next line from the input file
        write the line to the output file
    }
    close the input file
}
close the output file

No need for seeks or tests for names etc.
 
Share this answer
 
Comments
Member 10093180 6-Jun-13 6:03am    
Thx, This process will be quite if I have 5,000,000 lines...
Richard MacCutchan 6-Jun-13 6:05am    
Just another reason to make us suspect that there is a lot more to this issue than you put in your original question.
Member 10093180 6-Jun-13 6:06am    
What do you mean?
Richard MacCutchan 6-Jun-13 6:28am    
I mean that you have not given all the information rleevant to your problem. Your original post states that you are trying to combine multiple input files (one at a time) into a single output file, using getline etc. Now you are adding more information that suggests there is other processing, and decision taking that affects how you process the data, but you have still not made it clear. Please edit your original question and show clearly (with code) how you are processing your data and where the problem lies.
Member 10093180 6-Jun-13 6:36am    
ok thanks mate, I will edit my post again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900