#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
//opening collection.txt using ptr
FILE *ptr;
char data[1000];
ptr = fopen("collection.txt", "r");
printf("Hello world \n");
fscanf(ptr, "%s", data);
printf("%s \n", data);
fclose(ptr);
return 0;
}
收藏.txt:
hi my name is
當我運行這個程式時,我得到:
Hello world
P7k
P7k 是我假設的記憶體位置。
我查看了多個網站和文章,但無法弄清楚如何列印 collection.txt 中的文本
uj5u.com熱心網友回復:
問題包括
不測驗fopen()成功
FILE *ptr;
// add
if (ptr == NULL) {
fprintf(stderr, "Fail to open\n");
return -1;
}
不測驗fscanf()成功
// fscanf(ptr, "%s", data);
if (fscanf(ptr, "%s", data) != 1) {
fprintf(stderr, "Fail to read\n");
return -1;
}
不限制輸入
char data[1000];
// fscanf(ptr, "%s", data);
fscanf(ptr, "
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361751.html
下一篇:C遞增指標地址傳遞給函式 運算子
