开发者

Reading characters from a stream up to whitespace using isspace in c

开发者 https://www.devze.com 2022-12-18 15:50 出处:网络
I\'m trying to write a function that 开发者_如何学Gotakes a stream as an argument and reads from that stream and reads in the contents of the file up to the first whitespace as defined by the isspace

I'm trying to write a function that 开发者_如何学Gotakes a stream as an argument and reads from that stream and reads in the contents of the file up to the first whitespace as defined by the isspace function, and then uses the strtok function to parse the string. I'm not sure how to start it though with which function to read a line and ignore the whitespace. I know fgetc and getc only read one character at a time, and looking up the fscanf reference, will that work? Or does that only find items in your stream according to the format specifiers %s? Thanks!


To read an entire line at a time, you generally should use fgets. Some care is needed in case a line in the stream is longer than your buffer, the leftover will remain in the stream, which might not be what you want. (If you want to ignore the rest of the line, you can use fgets followed by fscanf as described at http://home.datacomm.ch/t_wolf/tw/c/getting_input.html.)

If you want to read in an entire line without worrying about a buffer size, you may wish to look into Chuck Falconer's ggets function which dynamically allocates a buffer for you (this does mean that you are responsible for freeing it).


C Answer

The fscanf s conversion will match a sequence of non-white-space characters. The input string stops at white space (as defined by isspace) or at the maximum field width, whichever occurs first. Note that there must be enough space in the provided buffer or it may be overflowed by long input.

FILE *fp;
char cstr[128];

fp = fopen("test.txt", "r");

while (!feof(fp))
{    
    fscanf(fp, "%s", cstr);
    ...
}

Original C Answer

The fgets function will allow you to read in the file one line at a time, but you will still need to check each character with isspace.

Since isspace may include space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v') in its check for white-space characters, your best bet may be to read one character at a time in a loop using the fgetc function. Note that if the integer value returned by fgetc() is stored into a variable of type char and then compared against the integer constant EOF, the comparison may never succeed, because sign-extension of a variable of type char on widening to integer is implementation-defined.

FILE *fp;
int c;

fp = fopen("test.txt", "r");
while ((c = fgetc(fp)) != EOF)
{
    if (isspace(c))
    {
        ...
    }
    else
    {
        ...
    }
}

Original C++ Answer

The istream::getline method will allow you to read in one line at a time and optionally specify the delimiter (default is '/n').

Since isspace may include space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v') in its check for white-space characters, your best bet may be to read one character at a time in a loop using the istream::get method.

char c;
string str;
ifstream file("test.txt",ios::in);
while (file.get(c)) 
{
    if (isspace((unsigned char)c))
    {
        ...
    }
    else
    {
        str.push_back(c);
    }

    file.peek();
    if (file.eof())
    {
        break;
    }
}

Note: Error checking was omitted from the all of the above code for simplicity.


well although getc/fgetc only retrieve 1 character at a time, you can put them into a loop right ?:)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号