Thread: Getline Function Problem

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    10

    Getline Function Problem

    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!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is my program running to fast,
    No.
    why getline would be skipping part of what things I want?
    Without seeing your code I have no idea.

    I suggest you post the relevant code, a sample of your input files. And then show us what the code you provide is producing and your desired output.


    Jim

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Hi Jim,

    Thanks for your reply

    I have a sample of my code, as you can see I have specific Datalength of where the place should start to getline for specific data I would like to extract

    For example, the datalength for a specific data is 240, but the output file will just grab 150 of the 240, but it is correct where the getline starts

    I have about 100+ CSV file containing, Date, Open, High, Low, Close Just standard price data
    Quote Originally Posted by jimblumberg View Post
    No.

    Without seeing your code I have no idea.

    I suggest you post the relevant code, a sample of your input files. And then show us what the code you provide is producing and your desired output.


    Jim
    Code:
    	if (dataname[i] == dataname)
    	{
    		inFile.seekg(datalength[i], ios::beg);
    		for (int j = 0; j < datacount[i]; j++)
    		{
    			getline(inFile, line);
    			outFile << line << endl;
    		}
    		
    	}
    And I do this for multiple files from use case 0 - Case 300 etc

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    I'm sure it is a kind of memory leak....

    Or is it using vector pushback and then output is the safer approach

    But how to outfile using vector which I don't know how to use

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, it is not a memory leak.

    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Grumpy!! u are the man!! Thanks!!

    Just want to know how can I use in different stream?

    Can I put the result in array then at different stream output it back that way? Meaning one is void and another in main?

    Quote Originally Posted by grumpy View Post
    No, it is not a memory leak.

    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    since you're writing to the file, seekg will not do what you expect anyway. it moves the "get" pointer (hence the 'g'), whereas seekp moves the "put" pointer.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    What do you mean? I don't get it, what you mean it doesn't work?

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    seekg goes with ifstream and/or fstream objects to move about within the file for reading.

    seekp goes with ofstream and/or fstream objects to move about within the file for writing.

    Why would I need seekp?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    One possibility: To move backwards in the file, for example to overwrite some data.
    Another possibility: If the OS allows it, you can seek beyond the end of the file and write there to automatically create a big file without having to fill it with garbage data. Useful for preallocation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    10
    Hi Elysia

    Thanks for your reply.

    I research these few past days, some people saying seekg() is used too often, and its not really good

    A better way is read the whole buffer and skipping the buffer, but this is really complicated, if anyone know it really works, I will move towards this direction.

    In the meantime, the problem of the output file is really consistent, the problem output is generated the same each time, the spacing is about 227 line each time for some data extracted from different files

    Code:
    char buffer[52];for (int i=0;i<n;i++) {    fid.read(buffer, sizeof(buffer));    memcpy(&x[i], &buffer[4], sizeof(x[i]));    memcpy(&y[i], &buffer[20], sizeof(y[i]));    // etc}
    Code:
    Last point
    
    if (dataname[i] == dataname)
    {
        inFile.seekg(datalength[i], ios::beg);
        for (int j = 0; j < datacount[i]; j++)
        {
            getline(inFile, line);
            outFile << line << endl;
        }
         system("Pause"); <---------------(If add this and press enter myself, the data is accurate and no spacing..... I don't get it!!"
    }
    

    Quote Originally Posted by Elysia View Post
    One possibility: To move backwards in the file, for example to overwrite some data.
    Another possibility: If the OS allows it, you can seek beyond the end of the file and write there to automatically create a big file without having to fill it with garbage data. Useful for preallocation.
    Last edited by neutral87; 06-06-2013 at 01:05 AM.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show a small complete program that illustrates your problem. Also post what you input into the program, and provide a small sample input file. Then show an example of the output your program is producing and what the desired output should be.

    Jim
    Last edited by jimblumberg; 06-06-2013 at 07:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with getline function
    By chickenlittle in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2010, 01:04 PM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. Is there anyway to use the getline function with strings?
    By Terranc in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2002, 12:02 PM
  4. getline() function
    By unanimous in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2001, 08:58 PM
  5. getline function
    By JamMan in forum C++ Programming
    Replies: 11
    Last Post: 11-25-2001, 05:29 PM