Read only a fixed column width in a text file in C

穿精又带淫゛_ 提交于 2019-12-12 03:16:33

问题


Let's say I have this text file data.txt:

|-- 20 characters--||-- 20 characters--|
Carlos              Rivera Bernal 
Thomas              James Hanner
James               Patrick Sullivan
Brayan              Williams
Khaterine           Smith
Ben                 Thompson

How could you read a text file with only the first column of fixed width. After storing each row in that column into a variable and use it as a parameter to a function.


回答1:


If your lines were consistently padded to 40 characters plus newline, you could use %20c to read each 20-character field. That would save the blanks and all; you could strip those off afterwards if you wish. Note that this does not null terminate the strings; you have to do that.

char first[21];
char last[21];

while (scanf(" %20c %20c", first, last) == 2)
{
    first[20] = last[20] = '\0';
    printf("[%s] [%s]\n", first, last);
}

With properly formatted data, this produces:

[Carlos              ] [Rivera Bernal       ]
[Thomas              ] [James Hanner        ]
[James               ] [Patrick Sullivan    ]
[Brayan              ] [Williams            ]
[Khaterine           ] [Smith               ]
[Ben                 ] [Thompson            ]

However, if the second field is not padded to (at least) 20 characters, the %20c will consume the newline and start of the next line:

[Carlos              ] [Rivera Bernal 
Thoma]
[s              James] [Hanner
James        ]
[Patrick Sullivan
Bra] [yan              Wil]
[liams
Khaterine     ] [Smith
Ben           ]

Your next choice, which works on the given data, is:

while (scanf(" %20[^ ] %20[^\n]", first, last) == 2)
    printf("[%s] [%s]\n", first, last);

It reads up to 20 characters (or the first blank) in the first field, then up to 20 characters (or the newline) in the second field. On my copy of your data, that produced:

[Carlos] [Rivera Bernal ]
[Thomas] [James Hanner]
[James] [Patrick Sullivan]
[Brayan] [Williams]
[Khaterine] [Smith]
[Ben] [Thompson]

There must be a trailing blank after Carlos's name and not anyone else's. You could combine the techniques to read a 'first name' that has blanks in it:

while (scanf(" %20c %20[^\n]", first, last) == 2)
{
    first[20] = last[20] = '\0';
    printf("[%s] [%s]\n", first, last);
}

which yields:

[Carlos              ] [Rivera Bernal ]
[Thomas              ] [James Hanner]
[James               ] [Patrick Sullivan]
[Brayan              ] [Williams]
[Khaterine           ] [Smith]
[Ben                 ] [Thompson]

You could write a function to null trailing blanks. For example:

#include <assert.h>
#include <stdio.h>

static void null_trailing_blanks(char *buffer, size_t buflen)
{
    assert(buflen > 0);
    buffer[--buflen] = '\0';
    while (buflen > 0 && buffer[buflen-1] == ' ')
        buffer[--buflen] = '\0';
}

int main(void)
{
    char first[21];
    char last[21];

    while (scanf(" %20c %20c", first, last) == 2)
    {
        null_trailing_blanks(first, sizeof(first));
        null_trailing_blanks(last, sizeof(last));
        printf("[%s] [%s]\n", first, last);
    }

    return 0;
}

On the fully-padded data set, that yields:

[Carlos] [Rivera Bernal]
[Thomas] [James Hanner]
[James] [Patrick Sullivan]
[Brayan] [Williams]
[Khaterine] [Smith]
[Ben] [Thompson]


来源:https://stackoverflow.com/questions/34759398/read-only-a-fixed-column-width-in-a-text-file-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!