Thread: C read colon delimited text file

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    C read colon delimited text file

    The code reads a text file delimited by colons : and formatted as follows
    1111:2222:3333
    How would I store the values separated by colons : into separate variables ?
    any help would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    int read_file();
    
    int main()
    {
    
    read_file();
    
    return 0;
    
    }
    
    int read_file()
    {
    char line[100];
    char file_location[40];
    
    FILE *p_file;
    
    printf("Enter file location: ");
    scanf("%s",file_location);
    
    p_file =fopen(file_location, "r");
    
    if(!p_file)
    {
        printf("\n File missing:");
        return 0;
    }
    
    while(fgets(line,100,p_file)!=NULL)
    {
        printf("%s \n",line);
    }
    
    fclose(p_file);
    
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could use strtok, I suppose; or if you know how many fields there are on a line (ie that there will always be four fields) you could use scanf (or in this case sscanf).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing pipe-delimited linesfrom a text file
    By JOhn12341234 in forum C Programming
    Replies: 5
    Last Post: 07-03-2013, 08:51 AM
  2. Read tab delimited file with selected output
    By Chang Kee Chung in forum C Programming
    Replies: 2
    Last Post: 06-28-2012, 09:49 AM
  3. Tab-delimited Text File
    By eezstreet in forum C Programming
    Replies: 13
    Last Post: 05-18-2011, 06:18 PM
  4. Comma delimited text sorting, advice?
    By rollyahh in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2011, 05:17 AM
  5. Simple comma delimited text file
    By zeebo17 in forum C Programming
    Replies: 10
    Last Post: 06-23-2010, 08:05 AM