Thread: Reading from file with specific widths.

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    Reading from file with specific widths.

    Hello,

    I have an input file that has 3 columns. The first column includes alphanum special characters and spaces as well. It looks like this:

    Code:
    @ED#@ 11 AAA
    !@ R$     22 BBB
    !@EDF    33 CCC
    EV? %    44 DDD
    Here is my code that I wrote to read such files, but obviously, having spaces in the first column makes this code with fscanf inapplicable. I know the width of every column, so I know the number of characters to read. Any suggestions on how I could go about this? Thanks!

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
            FILE *in, *out;
            char one[11], two[33], three[53];
            int i=1;
    
            in = fopen(argv[1],"r");
            out = fopen(argv[2],"w");
            while(fscanf(in,"%s  %s  %s  ", one, two, three) != EOF)
            {
                    printf("Line %d: One:%s Two:%s Three:%s\n", i++,one, two, three);
                    sleep(1);
                    //fprintf(out,"insert %s %s%s\n", one, two, three);
            }
            fclose(in);
            fclose(out);
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have a few options. The first is to use %c with a width modifier, e.g. %10c will always read 10 chars (if there are that many), but will NOT null terminate the buffer it read into You have to pass it a char * to a buffer that can hold all that plus a null, and null terminate it yourself. The second is to use %[ with a width modifier to grab, say, everything but a newline: "%10[^\n]". That will read 10 chars, so long as there is no newline in those 10 chars, and store it in a buffer you pass in. That will also null terminate it for you. A third option would be read in a line with fgets, and strncpy each part into it's own buffer. That came from the top of my head, so you probably want to double check it against the scanf docs when you get a chance.

    Also, you need to check that your fopen calls succeeded before trying to read from or write to them:
    Code:
    if (in == NULL) {
        perror("Couldn't open input file");
        // return error
    }

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    That helps, Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Widths help
    By Gart08 in forum C Programming
    Replies: 6
    Last Post: 11-27-2010, 08:26 PM
  2. Reading a specific line from a text file
    By acidrain in forum C Programming
    Replies: 3
    Last Post: 12-01-2009, 02:23 PM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Replies: 6
    Last Post: 08-04-2003, 10:57 AM
  5. Need help reading in specific type of file
    By Natase in forum C Programming
    Replies: 4
    Last Post: 09-12-2001, 08:02 PM

Tags for this Thread