Thread: Another problem with ftream

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    9

    Question Another problem with file i/o

    Well here I am asking yet another question, =\
    So what I need this time is help getting a string to hold each line of a .txt document. I already wrote something to tell me the number of lines, but now I'm lost as how to go about writing this part of my program. My first thought would be a loop but I don't know. Anyone have any ideas?
    Last edited by crateofrum; 01-09-2007 at 10:01 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I already wrote something to tell me the number of lines,
    You don't need to know the number of lines, you just need to keep reading lines until there are no more lines to read. The basic setup for reading from a file is to create a while loop and then use the read statement as the while conditional:
    Code:
    while(getline(inputFile, myStr) );
    {
        //do something with myStr
    }
    When you get to the end of the file the while loop will terminate. The while loop will also terminate if any errors occur while reading from the file, so you won't end up getting stuck in an infinite loop--which is not the case if you use:
    Code:
    while(!myFile.eof() )
    {
       getline(inputFile, myStr);
       
       //do something with myStr
    }
    Last edited by 7stud; 01-09-2007 at 08:29 PM.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    Quote Originally Posted by 7stud
    You don't need to know the number of lines, you just need to keep reading lines until there are no more lines to read. The basic setup for reading from a file is to create a while loop and then use the read statement as the while conditional:
    Code:
    while(getline(inputFile, myStr) );
    {
        //do something with myStr
    }
    Unfortunately that doesn't quite work the way I need it to. I know how to do that, But I need each line to become assigned to a string. I was thinking something maybe like :
    Code:
    string item1, item2, something;
    int x;
    void Stock::GetNames(char *file, int lines)
    {
         ifstream productlist(file);
         if(productlist.is_open())
         {
         while((getline(productlist, something)) && (x<lines))
             {
                        x++;
                        switch(x)
                        
                             case 1:
                             item1 = something;
                             break;
                              
                             case 2:
                             item2 = something;
                             break;
                             
                             // I don't want to have to add more string variables 
                             //than I absolutely have to.
                             default:
                             cout<<"Error!\n";
                             break;
                                                                                                                
             }
         }
         else
         {
             cout<<"Invalid File\n";
         }
    return item1, item2;
    }
    The problem with this code is that if i were to ever add more "items" to the .txt i would have to recompile the .exe with another item# variable. Is there a way of avoiding a situation like that?
    Last edited by crateofrum; 01-09-2007 at 09:55 PM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Yes, use either std::vector or std::list.

    Code:
    std::list<std::string> LineList;
    while(std::getline(productlist, something))
        LineList.push_back(something);

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Unfortunately that doesn't quite work the way I need it to. I know how to do that, But I need each line to become assigned to a string.
    Of course the code I posted does that: it assigns each line to the string myStr. It sounds like what you really want to do is assign each line to a different string. Note: if you don't clearly state what you want to do, you won't get relevant answers to your questions.

    Have you ever heard of arrays? Arrays are used to store a group of things, and they are a basic feature of most programming languages. Arrays and loops go hand in hand:
    Code:
    string lines[10];
    
    for(int i = 0; i < 3; i++)
    {
    	cout<<"Enter some words: ";
    	getline(cin, lines[i]);
    }
    
    
    cout<<"The first line was: "<<lines[0]<<endl;

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by 7stud
    Have you ever heard of arrays? Arrays are used to store a group of things, and they are a basic feature of most programming languages.
    std::vector is better to use here - because OP does not know the number of lines to be stored (at the beginning) Vector does all the dirty work of the dynamic allocation, not requiring to scan the file twice.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by vart
    std::vector is better to use here - because OP does not know the number of lines to be stored (at the beginning) Vector does all the dirty work of the dynamic allocation, not requiring to scan the file twice.
    Sometimes baby steps are more appropriate. Not everyone is able to understand Accelerated C++.
    Last edited by 7stud; 01-10-2007 at 01:07 AM.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by 7stud
    Sometimes baby steps are more appropriate. Not everyone is able to understand Accelerated C++.
    If somebody starts learning C++ as a first language - he should learn C++.

    Learning C just do make learning C++ easier in the future makes no good.

    vector replaces arrays as string replaces C-strings. It is simple. Start with the C++ aproach. When you are fluent with it - go back to the low level C-programming to improve your background understanding of what is going on out of the scene.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM