Thread: Using struct to pull records from a colon delimited file

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    2

    Using struct to pull records from a colon delimited file

    Hi guys,

    I'm trying to write a C program that will take a file that is delimited by colons and separate it out in to 5 sections:

    0002:0003:0002:0080:<HTML>
    0002:0004:0002:0080:<BODY>


    I have the struct code in place but not sure how to actually store each of the 5 parts in memory. Here is the code below:

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #pragma warning(disable:4996)
    
    
    struct contents
    {    
    int source,destination,type,port;   
    char data[50];
    };
    
    
    struct contents s,d,t,p,x;
    
    
    int main()
    {
    
    FILE*inFile;  
    charinFileName[30]={'\0'};
    
        
    printf("Enter file name: ");   
    scanf("%s",inFileName);   
    printf("\n");   
    printf("Opening %s . . .\n",inFileName);   
    printf("\n");
       
    if((inFile=fopen(inFileName,"r"))==NULL)   
    {       
    printf("Error: Could not open %s\n",inFileName);       
    exit(1);   
    }
    
       
    /* ...and now I'm stuck */
    
    
        
    fclose(inFile);
    
        
    return 0;
    
    }
    
    Any help would be greatly appreciated.

    Haze

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to format your code properly, especially by using proper indentation. For example:
    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #pragma warning(disable:4996)
    
    struct contents
    {
        int source, destination, type, port;
        char data[50];
    };
    
    struct contents s, d, t, p, x;
    
    int main()
    {
        FILE *inFile;
        charinFileName[30] = {'\0'};
    
        printf("Enter file name: ");
        scanf("%s", inFileName);
        printf("\n");
        printf("Opening %s . . .\n", inFileName);
        printf("\n");
    
        if ((inFile = fopen(inFileName,"r")) == NULL)
        {
            printf("Error: Could not open %s\n", inFileName);
            exit(1);
        }
    
        /* ...and now I'm stuck */
    
        fclose(inFile);
    
        return 0;
    }
    (I did not put in any extra syntax highlighting; the forum software will do syntax highlighting for you so you should not add in your own like what you did.)

    You should avoid global variables. Furthermore, you should use descriptive names for your variables, especially when they are not within some small scope.

    Now, if you want to read in the filename with scanf, that can be done, but you need to avoid buffer overflow by specifying the field width. You should also check the return value of scanf. For example:
    Code:
    if (scanf("%29s", inFileName) != 1)
    {
        printf("Error: invalid filename\n");
        return 0;
    }
    Next, to actually read from the file, it looks like you should read line by line. It seems like each line will have 20 + N characters, where N is up to a maximum of 49, seeing that you're going to use a 50 character array to store that 5th part. If so, you could use say, a 71 character array (20 + 49 + 1 for newline + 1 for null character), reading into it using fgets, then parse the line read into a struct contents object with sscanf. You might want an array of struct contents objects, or perhaps a single one will do, depending on your requirements (but whether array or lone object, it should be local to the main function).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    2
    Thanks for your reply.

    The code is indented, I literally copied/pasted from Visual Studio and it messed up the formatting!

    That last paragraph is where I'm stuck, I'm not sure how to code that part...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried? Have you looked up the use of fgets and sscanf?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C read colon delimited text file
    By CodeBug in forum C Programming
    Replies: 1
    Last Post: 11-30-2013, 11:06 AM
  2. Replies: 13
    Last Post: 11-18-2009, 01:05 AM
  3. pull out data frm a text file based on a condition
    By nirmala.s in forum C Programming
    Replies: 21
    Last Post: 11-27-2006, 12:56 AM
  4. fread records into a struct
    By Machiaveli in forum C Programming
    Replies: 1
    Last Post: 10-14-2006, 03:35 PM
  5. display records held in a struct
    By colinuk in forum C Programming
    Replies: 3
    Last Post: 02-02-2005, 07:51 AM