Thread: Open the text file and read the content line by line and put into array

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    4

    Open the text file and read the content line by line and put into array

    Hi
    I am writing the c code to open text file, read the line and break the string if there is a white space and put them into two different arrays.

    But I want to know how to detect the blank line and skip to another line and do the same thing. Below is my code. Attached is the text file that I want to get the data from

    Code:
    void main(void)
    {
        char buffer[1000] ;   
        char *line;   
        char *token;
        char *breaks = "  ";
        
        
        FILE *fstream = fopen("try1.txt","r");   
        if(fstream == NULL)   
        {      
           printf("\n file opening failed ");      
        } 
        
        
        do
        {
          line=fgets(buffer, 100000, fstream);
          printf("\nline = %s", line);
          
         /*  token = strtok(line, breaks);
          while(token != NULL)
          {
             printf("\ntoken = %s", token);
             token = strtok(NULL, breaks);
          } */
          
          token = strtok(line, breaks);
          printf("\ntoken = %s", token);
          
          token = strtok(NULL, breaks);
          printf("\ntoken = %s", token);
          
          token = strtok(NULL, breaks);
          //printf("\ntoken = %s", token);
          
        }
        while (line != NULL);
        
        fclose(fstream);
      
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Be consistent with your indentation.

    Don't put newlines BEFORE the strings you are printing. That is bizarre. Normally we put them after.
    Code:
    // Don't forget the header files.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // main should be declared to return an int
    int main(void) {
        // A separate "buffer" string and "line" pointer are not needed.
        char line[1000];
        char *token;
        // Don't use actual tabs in a string. Use \t instead.
        // You also need \n since fgets leaves it at the end of the string.
        const char *breaks = " \t\n";  // and it should be const since it's pointing to a string literal
    
        FILE *fstream = fopen("try1.txt", "r");
        if (fstream == NULL) {
            // perror prints the reason for the failure
            perror("fopen");
            // If the file doesn't open, you should exit.
            exit(EXIT_FAILURE);
        } 
    
        // Don't put a random line size here, especially if it's greater than the actual string.
        // Usually we use the actual string length.
        // And testing if the fgets return value is NULL at the end of the loop is too late since
        // you've already tried to get tokens from it with strtok. So test it here.
        while (fgets(line, sizeof line, fstream) != NULL) {
    //        printf("line = %s", line);
            token = strtok(line, breaks);
            if (token != NULL) { // skip empty lines
                printf("token = %s\n", token);
                token = strtok(NULL, breaks);
                printf("token = %s\n\n", token);
            }
    
            // Why would you do this a third time if there's
            // only two tokens on the line?
    //        token = strtok(NULL, breaks);
        }
    
        fclose(fstream);
    
        // You should explicitly return 0 at the end of main to indicate successful completion.
        return 0;
    }
    Last edited by algorism; 05-09-2017 at 10:22 PM.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    4
    Thanks for you explanation and code. If I follow the same the program will also print the empty token as shown below. But I dont want to print that. How can I change the code?


    token = 0012E02B00
    token = 0F80

    token = 0012DA2398
    token = 8

    token = 0012E02B00
    token = 0F80

    token = 0012DA2398
    token = 8

    token =
    token =<?>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read a text file line by line?
    By Sam Conran in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2016, 09:22 AM
  2. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  5. how do you read a whole line of text from a txt file
    By themexican in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2005, 09:17 AM

Tags for this Thread