Thread: I/O help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    22

    I/O help

    I am working on an exercise that has me ask the user for the name of a text file and then display the just the first 10 lines of it. If there is 10 lines or less, I have to tell the user the entire file has been displayed. This is what I have written.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	fstream dataFile;
    	string buffer;
    	string filename;
    	int count;
    
    	cout << "Please enter the name of the file: ";
    	getline(cin,filename);
    	dataFile.open(filename.data(), ios::in);
    	if (!dataFile)
    	{
    		cout << "The file " << filename << " could not be opened" << endl;
    		exit(1);
    	}
    	for (count = 1; count < 9; count++);
    	getline(dataFile,buffer);
    	cout << buffer[count] << endl;
    
    	dataFile.close();
    }
    I know there is a way to do it by just typing
    Code:
    	getline(dataFile,buffer);
    	cout << buffer[count] << endl;
    10 times but I want to do it this way. If I take out the loop the program works so I know that is the only problem.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Currently your for-loop does the statement ";" nine times. You probably want it to do the statement
    Code:
    {
    getline(dataFile,buffer);
    cout << buffer[count] << endl;
    }
    instead.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    Quote Originally Posted by tabstop View Post
    Currently your for-loop does the statement ";" nine times. You probably want it to do the statement
    Code:
    {
    getline(dataFile,buffer);
    cout << buffer[count] << endl;
    }
    instead.
    If I do that it just prints the third character on the line.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because I wasn't paying attention and/or I copied your code. It actually gives you the first character of the first line, the second character of the second line, etc... You should just print buffer whole.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    Quote Originally Posted by tabstop View Post
    That's because I wasn't paying attention and/or I copied your code. It actually gives you the first character of the first line, the second character of the second line, etc... You should just print buffer whole.
    Ok, thanks. I was just trying to challenge myself.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    Now I'm having another problem. I just repeated
    Code:
    	getline(dataFile,buffer);
    	cout << buffer[count] << endl;
    but when I do that it will always print 10 lines and will repeat the last line of the file if it is less than 10 lines.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Put "count < 10 && !getline(dataFile,buffer)" in the condition of the for loop. That way the loop will break when the count is ten, or when an error occurs when reading from the file (such as the file having less than 10 lines).
    Last edited by King Mir; 04-23-2008 at 07:04 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    22
    Is this what you want me to do?
    Code:
    	for (count < 10 && !getline(dataFile,buffer));
    	getline(dataFile,buffer);
    	cout << buffer[count] << endl;
    This gives my syntax errors. It says missing ';' before ')' on the line the for loop is on.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    By "in the condition of the for loop" I meant between the two semi colons. So replace your "count < 9" statement.

    Also, you'd need to remove it from the for loop body.

    And tabstop is correct in his remark. There's just a lot of other things wrong with your code.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    for (count = 1; count < 10 && !getline(dataFile,buffer); count++)
    {
        //...
    }
    That's how for should look like.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. Nonblocking I/O
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 11-29-2005, 04:37 PM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  5. WSAAsyncSelect I/O Mode :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2002, 03:23 PM