how to set text file into a string?

This is a discussion on how to set text file into a string? within the C Programming forums, part of the General Programming Boards category; can anyone help? thanks Code: /* Reading from the script file */ #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <ctype.h> ...

  1. #1
    apm
    apm is offline
    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 08:54 PM.

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

    For example http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

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

    You shold look for strcmp
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 01:13 AM
  3. storing text file contents into a string
    By m.mixon in forum C Programming
    Replies: 4
    Last Post: 07-20-2006, 12:52 PM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21