0

そのため、テキスト ファイル内の段落 (単語) をスキャンするために fscanf を使用する必要があり、次のコードを記述しました。

コードスニペット:

 char foo[81];
 char *final[MAXIUM]; //this is another way of making a 2d array right?
 int counter=0; 

 while (counter<MAXIUM && fscanf(file, "%s", foo)!= EOF){ 
   *final = (char*)malloc(strlen(foo)*sizeof(foo)); 
   //if (final ==NULL) 
   *(final + counter ) = foo + counter;

   counter++;
 }

テキスト ファイルは、古い段落のように見えます。

しかし、同社は顧客のソーシャル メディア クエリにまだ応答していません。あなたは熱心なソーシャル メディア愛好家またはそのファンである可能性があります。


このコードの主なポイントは、%s と fscanf のみを使用してテキスト ファイルから段落をスキャンし、各単語に十分なスペースを割り当てて最終的に配置することです (foo はスキャン ビットのためだけに一時的なものである必要があります)。 MAXIUM を通して読み取られる最大単語数はわかっています。

ご助力いただきありがとうございます:

4

2 に答える 2

1

変化する

while (counter<MAXIUM && fscanf(foo, "%s", foo)!= EOF){
  *final = (char*)malloc(strlen(foo)*sizeof(foo)); 
  *(final + counter ) = foo + counter;
....
 for(counter=0; i<MAXIMUM; counter++) printf("%s",final[counter])

// Also recommend that the first thing you do is fill your `final[]` with NULL, 0
for (int j=0; j<MAXIUM; j++) final[j] = 0;

// changed fscanf to fgets.  Less issues with dropping whitespace.
while ((counter<MAXIUM) && (fgets(foo, sizeof(foo), stdin)!= EOF)){ 
  final[counter] = (char*)malloc(strlen(foo)+1);  // some say (char*) cast is undesirable, bit allowed.
  strcpy(final[counter], foo);
  // eliminate *(final + counter ) = foo + counter;  
...
for(i=0; i<counter; i++) printf("%s",final[i]);  // The fgets() will preserve the EOL 
于 2013-06-04T02:28:02.253 に答える