Question on i/o streaming(streams)

This is a discussion on Question on i/o streaming(streams) within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <fstream.h> #include <conio.h> #include <string> #define file "C:/Folder/File.txt" int main() { string word=""; ofstream a(file); a ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    Question on i/o streaming(streams)

    Code:
    #include <iostream>
    #include <fstream.h>
    #include <conio.h>
    #include <string>
    #define file "C:/Folder/File.txt"
    
    int main()
    {
     string word=""; 
     ofstream a(file);
     a << "This is the first line." << endl;
     a << "This is the second line." << endl;
     a << "This is the third line." << endl;
     a << "This is the fourth line." << endl;
     a.close();
    
     ifstream x(file);
     while (x >> word) {
      if (!x.eof()) {
      cerr << word << " "; 
      x.seekg(NULL, ios::beg);
      x.clear(); }
      
      else {
      x.close(); }
      }
      x.close();
     
      cin.get();
      return 0;
    }
    How do you read the file line by line?
    How do you go to a specific line in the text file and read it?(like go to line 4 and read the text there)
    Is there anything I could put besides NULL in the first parameter of seekg?
    Last edited by Golden Bunny; 05-12-2002 at 09:31 PM.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How do you read the file line by line?
    Code:
    while ( x.good() ) {
      std::getline ( x, word );
      // Do whatever with the line
    }
    >How do you go to a specific line in the text file and read it?(like
    >go to line 4 and read the text there)
    By starting at the beginning and reading three lines. Text files aren't very forgiving in how you can move through them.

    >Is there anything I could put besides NULL in the first parameter of seekg?
    Yes, you can use any offset from 0 to the size of the file if you are in binary mode, otherwise yes, but you can't be guaranteed that it will work. Chances are that it will not.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about file I/O.
    By The7thCrest in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 05:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 01:46 PM
  4. File I/O Question
    By 182 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2005, 03:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM

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