Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


please help reading a file

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Fri Apr 23, 2010 7:53 am    Post subject: please help reading a file Reply with quote

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
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Apr 23, 2010 9:53 am    Post subject: Re: please help reading a file Reply with quote

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
View user's profile Send private message
kot1990
Expert Cheater
Reputation: 1

Joined: 06 Sep 2009
Posts: 131
Location: Greece

PostPosted: Fri Apr 23, 2010 10:19 am    Post subject: Reply with quote

thx
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Apr 23, 2010 1:25 pm    Post subject: Reply with quote

I'd recommend using fgets() and sscanf() over fscanf().
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites