2016-05-17 156 views
0

我試圖使用下面的代碼掃描文件中的字符串。但我的程序打印怪異的字符。任何想法如何阻止這種情況,以及如何在打印字符串時在單詞之間保留空格?從文件中掃描C

這裏是文件(test.txt的)的內容  (test.txt)

這裏是我的程序的輸出:

output

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct 
{ 
    char word[80]; 
    int length; 
    int freq; 
} sent; 

int main() 
{ 
    sent a[50]; 
    int v,status; 
    int i=0,cnt=0; 
    char*y; 

    FILE*p; 
    p=fopen("C:\\Users\\User\\Desktop\\test.txt","r"); 
    status=fscanf(p,"%s",a[i].word); 
    while(status !=EOF){ 
     i++; 
     status=fscanf(p,"%s",a[i].word); 
    } 
    for(i=0;i<50;i++) 
    { 
     char *y=strtok(a[i].word,"[email protected]#$%&*?."); 

     while(y!=NULL) 
     { 
     printf("%s",y); 
     y=strtok(NULL,"[email protected]#$%&*?."); 

     } 
    } 
} 
+6

請張貼test.txt的內容。我的猜測是你文件中沒有50個單詞。 –

+0

fscanf(p,「%s」,a [i] .word);可以以緩衝區溢出結束,使用fscanf(p,「%79s」,a [i] .word);或fgets()。並在fopen() – 12431234123412341234123

回答

0

這只是字符串操作。我調試並修改了一下程序,以便有文本輸出而不是垃圾。您可能需要再修改一下,但它現在會打印文件的內容。你得到垃圾字符的原因是,當字符串沒有被終止時,循環不知道何時停止,所以你從其他東西獲得內存內容。推薦的方法是fgets來讀取文件並保留空白。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct { 
    char *word; 
    int length; 
    int freq; 
} sent; 

int words(const char *sentence) { 
    int count, i, len; 
    char lastC; 
    len = strlen(sentence); 
    if (len > 0) { 
     lastC = sentence[0]; 
    } 
    for (i = 0; i <= len; i++) { 
     if (sentence[i] == ' ' && lastC != ' ') { 
      count++; 
     } 
     lastC = sentence[i]; 
    } 
    if (count > 0 && sentence[i] != ' ') { 
     count++; 
    } 
    return count; 
} 

int main() { 
    sent a[50]; 
    int v, status; 
    int i = 0, cnt = 0; 
    FILE *p; 
    p = fopen("data.txt", "r"); 
    char buf[100], title[100]; 
    fgets(buf, sizeof buf, p); 
    int j = words(buf); 
    char *yy; 
    yy = strtok(buf, "[email protected]#$&*?%."); 

    while (yy != NULL) { 
     a[i].word = yy; 
     yy = strtok(NULL, "[email protected]#$&*?%."); 
     i++; 
    } 

    for (int k = 0; k<i; k++) { 
     printf("%s", a[k].word); 
    } 
} 

該程序標記緩衝區並保留空白。我將你讀取文件的方式改爲fgets

的data.txt

其規模從一到十$什麼是你最喜歡的字母

輸出

的顏色從一個到規模十你最喜歡什麼顏色的字母表

+2

之後檢查錯誤請描述您對OP的代碼所做的修改,以便它適用於您。 –

+0

@RSahu我加入了改進的代碼和更多描述。我希望你再次批准或評論。 –

+0

@ Programmer400 - 你沒有提到爲什麼你的代碼更好,或爲什麼他的代碼失敗...... – Soren

2

由於人們評論說,你很可能沒有你讀文件中的50個字,但你的循環超過50試圖環......反正,所以這行

for(i=0;i<50;i++) 

應該進行修改,以

int w; 
for(w=0;w<i;w++) 

並且您應該在循環內替換使用iw(或者您打算在while循環內使用變量cnt,因爲該代碼中當前未使用該變量)。

而且您需要保護緩衝區溢出,如果您的文件包含超過50個詞的話會發生,但這超出了本答案的範圍。

更新回答您的評論:

對有字與字之間的空間,你只是簡單地將它們添加到輸出,就像

printf("%s ",y); 

你的scanf但是將終止字符串掃描任何空格,所以空格(十六進制20),換行符(\ n),製表符(\ t)或返回符號(\ r)將全部爲您的字符串的終止字符 - 如果您想保留並輸出相同的內容,對於那些人也如

char theString[50]; 
    char theSpace; 
    int matched = scanf("%s%c",theString, theSpace); 

,如果匹配== 2,那麼你已經掃描這兩個字符串和終止掃描的空間,並且可以打印它像

printf("%s%c",theModifiedString,theSpace); 
+0

所以'我'在這裏是指單詞的數量,而不是文件中字符的數量.....我計算了文件中的單詞,並根據數字更改了循環...它的工作,但沒有空格之間的話我怎麼能保持他們之間的空間?? ....謝謝你的答案它幫助 –

+0

查看更新的答案 – Soren

+0

如果我使用'printf(「%s」,y);'它不會工作,因爲我在這個單詞本身的字符之間有特殊字符,所以這個單詞中的字符之間會有空格。 –