0

假设我有一个 char string[20];

如果我想使用 fscanf 从文件中读取字符串,并且因为如果我使用 fscanf(),第一个字符总是会被跳过,

然后我将执行以下操作:

string[0] = x //where x is the char from fgetc();

然后我将调用 fscanf,它将填充剩余的字符串 [1-19],例如,如何在不使用 stringcat 的情况下将其存档?

我尝试了类似的东西

*string++;// but this give me a left operand error

例如:

输入:

你好 123 1.2 '\n' 再见 124 0.02

代码:

    while  ( ( y = fgetc( file2 ) ) != EOF )
    {
        if(y != '\n')
        {
            fscanf(file2,blah blah);//I scanf the string, the int and the double
        }
        else
        {
            printf(); // I will get everything on the line without the first char
        }
    }
4

1 回答 1

0

如果fscanf()要从数组的元素 1 开始读取字符串,则可以使用以下代码:

fscanf(file2, "%18s", string+1);

string是一个数组对象,而不是一个指针,所以你不能使用string++ 数组对象的操作。因为这相当于:

string = string + 1;

在这里,您为数组对象分配了一个 C 中不允许的新值。

从元素 1 访问字符串数组的方法是使用string + 1(不将其分配给字符串数组)。string + 1返回指向string数组元素 1 的指针

于 2013-05-09T04:44:49.913 回答