[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.
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;
}