question on reading from a file

This is a discussion on question on reading from a file within the C++ Programming forums, part of the General Programming Boards category; I need to read some info into a couple of stacks. How can I read different info into each stack ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    question on reading from a file

    I need to read some info into a couple of stacks. How can I read different info into each stack using only 1 input file. Is there a way to signal the program to only read for example two lines then the next two lines are for the next stack?? If I use an end of file condition everything is read into the first stack, correct?? I wasn't given option to set the size of the stack to only fit the data need so both stacks are the max size. The stacks are going to be filled with characters so a sentinel value has not worked.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Can't you do something like...

    // Psuedo-Code..
    Code:
    While( not done )
    {
      for( i = 0; i < numForStackA; ++i )
      {
        read line
        put in stackA
      }
    
      for( ....
      {
        read line
        put in stackB
      }
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    What is the code in C++ for read 1 line? I know read till end of file?? How do you just get 1 line???

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    I believe there are a number of ways but one way of doing it is to:
    Code:
    ifstream read("filename");
    char ch;
    while(read.get(ch))
    {    
        if(ch == '\n')
            break;
        cout << ch;
    }
    Another way is
    Code:
    ifstream read("filename");
    char ch[100];
    read.getline(ch);
    cout << ch;

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    ifstream read("filename");
    char ch;
    while(read.get(ch))
    {    
        if(ch == '\n')
            break;
        cout << ch;
    }
    Of course, such low level solutions should be avoided except where they actually improve the readability and reliability of the program. However, since the more abstract methods are already debugged and tested, they should be tried first.
    Code:
    ifstream read("filename");
    char ch[100];
    read.getline(ch);
    cout << ch;
    Speaking of low level, C-strings are a pain to work with. Pretty much the only people who use them in C++ are die-hard C programmers that are completely comfortable with the issues, and beginners that don't know any better or are denied the better solutions by their teachers. This is a better way to get a line:
    Code:
    ifstream read ( "filename" );
    string line;
    if ( getline ( read, line ) ) {
      // Use line
    }
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    Yes as I said, there are probably a number of ways of doing it, those were just the 2 I could think of.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >those were just the 2 I could think of
    You have another to add to your list now.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    hehe yup, just have to get more comfortable with the string class

  9. #9
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    strings are very good. look up cppReference
    and get the function listings. Also, you can use the + operator to concatenate, and the = operator to assign. no more strcat() or strcopy() again for me..... string looks nicer than char [] as well.
    Ph33r the sphericalCUBE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21