 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Fri Apr 23, 2010 7:53 am Post subject: please help reading a file |
|
|
I have a file that has data in it like this
Code: |
1 1 172 blabal blabla
1 1 173 blabal blabla 500
1 1 174 450 10 blabla
|
I need only the third value of each line, 172,173,174.
I tried the following:
Code: |
FILE* myfile = fopen("C:\\myfile","r");
fprintf(myfile," "); //2 spaces to skip the first 2 values.
fscanf(myfile,"%s",consoleBuffer);
fprintf(myfile,"\n"); <-- this one doesn't work.
|
Note: the file has not the same number of elements in each line.
EDIT: Solved
Code: |
char fileLine[500];
int ID;
FILE* myfile = fopen("C:\\myfile","r");
for(int i=0;i<10;i++)
{
fgets(fileLine,500,myfile);
sscanf(fileLine,"%*s %*s %d",ID);
sprintf(consoleBuffer,"%d\n",ID);
WriteConsole(consoleHandle,consoleBuffer,strlen(consoleBuffer),NULL,NULL);
}
|
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Fri Apr 23, 2010 9:53 am Post subject: Re: please help reading a file |
|
|
kot1990 wrote: | I have a file that has data in it like this
Code: |
1 1 172 blabal blabla
1 1 173 blabal blabla 500
1 1 174 450 10 blabla
|
I need only the third value of each line, 172,173,174.
I tried the following:
Code: |
FILE* myfile = fopen("C:\\myfile","r");
fprintf(myfile," "); //2 spaces to skip the first 2 values.
fscanf(myfile,"%s",consoleBuffer);
fprintf(myfile,"\n"); <-- this one doesn't work.
|
Note: the file has not the same number of elements in each line. |
Code: | #include <stdio.h>
int main() {
FILE * pFile;
if( !( pFile = fopen( "C:\\myfile.txt", "r" ) ) ) {
perror( "The following error occured" );
return 1;
}
else {
int num1, num2, num3;
char c;
do {
c = ' ';
int offset = fscanf( pFile, "%d\t%d\t%d", &num1, &num2, &num3 );
printf( "%d\n", num3 );
while( c != 0xa && c != EOF )
c = getc( pFile );
}
while( c != EOF );
fclose( pFile );
}
return 0;
} |
I copied and pasted your file and the new line was 0xa. However it could well be something else so that might be something you want to check for. Alternatively, you can read the entire file as a string and use strtok()
|
|
Back to top |
|
 |
kot1990 Expert Cheater
Reputation: 1
Joined: 06 Sep 2009 Posts: 131 Location: Greece
|
Posted: Fri Apr 23, 2010 10:19 am Post subject: |
|
|
thx
|
|
Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Apr 23, 2010 1:25 pm Post subject: |
|
|
I'd recommend using fgets() and sscanf() over fscanf().
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|