reading second line

This is a discussion on reading second line within the C++ Programming forums, part of the General Programming Boards category; Code: #include <fstream> #include <iostream> using namespace std; int main() { char str[10]; //Creates an instance of ofstream, and opens ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    70

    reading second line

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "example.txt" );
      // Outputs to example.txt through a_file
      a_file<<"This text will now be inside of example.txt\n";
    a_file<<"second line";
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }

    now i print the word THIS and if i do it again i will show TEXT , but how do i read the second line???

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    getline().
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    Code:
    24 C:\Documents and Settings\Roel Postelmans\Mijn documenten\teste.cpp
     no matching function for call to `getline(int)'
    how do i use it??

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    mmm
    when i do cin.getline(source ,2);
    i still get the second word

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    #include <string>
    
    ...
    
    string temp;
    
    ...
    
    getline(b_file,temp);  // Read whole 1st line from file into string variable "temp"
    cout << temp << endl;  // Output whole 1st line to "cout"
    
    getline(b_file,temp);  // Read whole 2nd line from file into "temp"
    cout << temp << endl;  // Output whole 2nd line to "cout"
    That's the string version of the getline function. If you wanted to still use character array's to do this, you could use the stream's getline member function instead (assuming you had a large enough array to store things in you could actually read a whole line in at a time).

    Code:
    const int line_len = 255;
    char line[line_len];  // Store a line from the file, make sure it's big enough
    
    ...
    
    b_file.getline(line,line_len);  // Read a single line from the input file
    cout << line << endl;  // Output that single line back to cout
    
    b_file.getline(line,line_len);  // Read the 2nd line from the input file
    cout << line << endl;  // Output that 2nd line back to cout
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    error:
    Code:
    17 C:\Documents and Settings\Roel Postelmans\Mijn documenten\teste.cpp no matching function for call to `getline(std::ifstream&, std::string[50])'

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    The string version of the getline function takes a string object as the second argument, not an array of strings. You do not need an array of strings to do what you are asking, just one will do (for now).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    70
    thnx but how do u copy files?

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Input all the lines and then ouput them all. Or call some function implemented by other people (I can't remember any).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  2. Replies: 1
    Last Post: 05-20-2006, 11:17 PM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Reading a user specificed line from a file
    By hslee16 in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2004, 05:58 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

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