Results 1 to 2 of 2

Thread: [RESOLVED] Reading Lines form file problum

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Resolved [RESOLVED] Reading Lines form file problum

    Hi i am trying to make a little program to execute commands
    My problum is when I read in a file for example like the one below
    at the end it adds an extra line

    Code:
    SET A,"Hello World"
    PAUSE
    Echo A
    So this is the code I use to read the above lines

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
    	
    	FILE *fp;
    	char buffer[50];
    
    	fp = fopen("C:\\out\\test.txt","r");
    
    	if(fp == NULL){
    		cout << "Cannot Open Filename\n";
    		return 0;
    	}
    
    	while(!feof(fp)){
    		fgets(buffer,50,fp);
    		//Write out each line
    		cout << buffer;
    	}
    
    	system("pause");
    }
    After running the code in the console it adds an extra line like below:

    Code:
    SET A,"Hello World"
    PAUSE
    Echo A
    Echo A <--This is the line it adds
    Can someone please help me thanks I am very new to c++ so i guess i am doing something wrong somewere thanks.

  2. #2

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: Reading Lines form file problum

    Ok i managed to fix it after spending almost yesterday figureing it out, anyway all it was was one line of code just needed to check if i was at the end of the file

    Code:
    if(!feof(fp)){
    //Write out each line
    cout << buffer;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width