关于C#:如何将字符串的一半添加到旧字符串?


how to add the half of the string to the old string?

假设我有一个
字符字符串[20];

如果我想使用fscanf从文件中读取字符串,并且由于我使用fscanf()时总是会跳过第一个字符,所以

然后我将执行以下操作:

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

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

我尝试过类似

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

例如:

输入:

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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
    while  ( ( y = fgetc( file2 ) ) != EOF )
    {
        if(y != '\
'
)
        {
            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
        }
    }


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

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

string是一个数组对象,而不是一个指针,因此您不能使用操作string++
用于数组对象。因为这等效于:

1
string = string + 1;

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

您可以从元素1访问字符串数组的方法是使用string + 1(无需将其分配给字符串数组)。 string + 1返回一个指向string数组的元素1

的指针