简体   繁体   中英

File I/O, fgets() and strtok() function in C

Inside the file.txt :

FIRST TYPEONE  
FIRST TYPETWO  
FIRST TYPETHREE  
SEC TYPETHREE  
FIRST TYPETWO  
FIRST TYPEONE  
SEC TYPETWO  
FIRST TYPETHREE  
FIRST TYPEONE  

When I execute the code, all "function()" doesn't work. First token (token#1) takes "FIRST", printf#1 print "FIRST" and enters the if condition. So far, it's okay. Token that is inside the if condition takes "TYPEONE" and printf#2 prints "TYPEONE". I think token keeps "TYPEONE" currently. But when i use strcmp and compare token and "TYPEONE" it doesn't enter this condition(condition#1) and so function() that is inside the condition#1 doesn't work and printf function doesn't print "type1" to screen.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_SIZE 200

int main()
{   
    char lineSize[MAX_LINE_SIZE];
    char *token;
    FILE *f = fopen("file.txt", "r");
    while (i< 9)
    {
        fgets(lineSize, MAX_LINE_SIZE, f); 
        token = strtok(lineSize, " "); //token#1

        printf("%s\n", token); //printf#1

        if( strcmp(token, "FIRST") == 0 )
        {   
            token = strtok(NULL , " "); //token#2

            printf("%s\n\n", token); //printf#2


            if( strcmp(token, "TYPETWO") == 0 )
                {function(); printf("type2\n");
                }
            else if( strcmp(token, "TYPETHREE") == 0 )
                {function(); printf("type3\n");
                }
            else if( strcmp(token, "TYPEONE") == 0 ) //condition#1
                {function(); printf("type1\n");
                }
        }

        else if( (strcmp(token, "SEC")) == 0  ) 
        {
            token = strtok(NULL, " ");

            if( (strcmp(token, "TYPETWO")) == 0 )
                {
                    function();
                    printf("type2\n");
                }

            else if( (strcmp(token, "TYPETHREE")) == 0 )
                {
                    function();
                    pritf("type3\n");
                }

        }
    }
}

I don't actually know what do you pretend with your sample, but it's ill formed. I've just made the minimum set of tests to the results of the functions you call to be able to compile and run the sample program you post. But anyway, I don't exactly see what do you want with it. PLEASE, post a complete and verifiable example of code (your i variable was neither defined, nor initialised anywhere, no function() definition anywhere, and no function return value check for errors anywhere also) What do you mean with function() does not work... no definition of function function() anywhere. How can we diagnose what happens with function() if you don't post it?

Next is the set of modifications you need to your program (in the form of a patch file, diff -u ) to make it runnable, but next time, please, check your code for being at least runnable to ask something here (the error messages where clear enough to be able to do it by yourself)

--- pru.c.orig  2018-03-12 09:03:54.000000000 +0200
+++ pru.c   2018-03-12 09:17:11.000000000 +0200
@@ -1,23 +1,41 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
+
 #define MAX_LINE_SIZE 200

+void function()
+{
+    printf("in function();\n");
+}
+
 int main()
 {   
     char lineSize[MAX_LINE_SIZE];
     char *token;
     FILE *f = fopen("file.txt", "r");
+    int i = 0;
+
+    if (!f) { /* CHECK FOR ERRORS */
+        fprintf(stderr,
+                "ERROR: fopen: file.txt: %s\n",
+                strerror(errno));
+        exit(1);
+    }
+
     while (i< 9)
     {
         fgets(lineSize, MAX_LINE_SIZE, f); 
         token = strtok(lineSize, " "); //token#1
+        if (!token) break; /* CHECK FOR ERRORS */

         printf("%s\n", token); //printf#1

         if( strcmp(token, "FIRST") == 0 )
         {   
             token = strtok(NULL , " "); //token#2
+            if (!token) break; /* CHECK FOR ERRORS */

             printf("%s\n\n", token); //printf#2

@@ -36,6 +54,7 @@
         else if( (strcmp(token, "SEC")) == 0  ) 
         {
             token = strtok(NULL, " ");
+            if (!token) break; /* CHECK FOR ERRORS */

             if( (strcmp(token, "TYPETWO")) == 0 )
                 {
@@ -46,9 +65,13 @@
             else if( (strcmp(token, "TYPETHREE")) == 0 )
                 {
                     function();
-                    pritf("type3\n");
+                    printf("type3\n");
                 }

         }
+        i++;
     }
+    if (i < 9) /* CHECK FOR ERRORS */
+        fprintf(stderr, "ERROR: less than 9 lines of input (%d).\n", i+1);
+    return 0;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM