2013-05-09 15 views
0

假設我有一個 char字符串[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 
     } 
    } 
+0

爲什麼第一個字符會被跳過? – 2013-05-09 04:27:33

+0

fscanf只讀取下一個字符是不是? – user1701840 2013-05-09 04:27:57

+0

不正確。 'man fscanf'和'man printf'。或者閱讀K&R。 ((y = fgetc(file))!= EOF) – 2013-05-09 04:28:52

回答

0

,如果你想要把從數組的元素1開始,那麼你可以使用下面的代碼由fscanf()讀取字符串:

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

string是一個數組對象,而不是一個指針,所以你不能用數組對象的操作string++ 。因爲這相當於:

string = string + 1; 

這裏你分配給數組對象未在C.

允許一個新值

你能做些什麼,從元素訪問您的字符串數組1使用string + 1(不將其分配給字符串數組)。 string + 1返回指向您的string陣列的元素1的指針