Thread: how to set text file into a string?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    9

    how do i make condition for string/int after tokenization

    can anyone help?

    thanks

    Code:
    /* Reading from the script file */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <ctype.h>
    //#include <iostream>
    #include <string.h>
    
    int main()
    {
       FILE *myFile;
       char filename[20];
    
          
       /* Ask user to enter the filname including the format */
       printf( "Enter the script filename with the format. ie script.txt\n" );
       scanf( "&#37;s", filename  );
       
       /* Open filename for Reading (only) */
       myFile = fopen(filename, "r");
    		if (myFile == NULL) {
    			printf("ERROR! Unable to open your file.\n");   
                printf( "Closing file.......\n" );   
                //delay two second
                sleep (2000);
    			exit(-1);
       }    
       /* Open file success message */
            else {
                 printf( "The file %s is valid\n\n", filename );
    }
                 
                 
        char string[500];
        fgets (string , 500 , myFile);
        int strcmp( const char *str1, const char *str2 );
        
        char *tokenPtr;
        tokenPtr = strtok( string , " " );
          while ( tokenPtr != NULL ) { 
          printf( "%s\n", tokenPtr );
          tokenPtr = strtok( NULL, " " );
    }
    
    
         //EXE Crashes here
     if ( strcmp ( tokenPtr, "RESET" ) == 0 ){
          printf ("\n Resetting the motors.");
          //outportb(0x378, 0); 
          }
          
    
    
    
       getch();
       return 0;
    }
    Last edited by apm; 06-05-2007 at 07:54 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Goto to FAQ
    look for fgets, fgetc, fread

    For example http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    errr, it's not changing my title name

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Don't edit the original post - post the updated code below...

    You shold look for strcmp
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. storing text file contents into a string
    By m.mixon in forum C Programming
    Replies: 4
    Last Post: 07-20-2006, 11:52 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM