簡體   English   中英

我如何將從文件中讀取的數據存儲在新數組中並打印新數組中的所有數據

[英]How i can store the data read from the file in new array and print all the data from the new array

因此,我已從文件中讀取數據並在顯示中打印,但我無法將從文件中讀取的數據保存到新數組中。

文件數據,例如:

23,56
78,65

我的代碼

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

int main() {
    FILE *ptr = fopen("datasetLR1.txt", "r");
    if(ptr == NULL) {
        printf("File not found or allocated!");
        exit(1);
     }
    int str[85];
    while(fgets(str, sizeof(str), ptr) != NULL) { 
        printf(fgets(str, sizeof(str), ptr));
    }
    system("pause");
    fclose(ptr);
    return 0;
}

您需要更改這些部分:

    char rowBuffer[85];  // fgets want's a char buffer
    while (fgets(rowBuffer, sizeof(rowBuffer), ptr) != NULL) {
        printf("%s", rowBuffer);  // We do not want to read and print again at the same time
    }

您的解決方案(還沒有)解釋 integer 值。

此外,正如其他人評論的那樣, printf(src)是不安全的,因為數據包含諸如“... %s ...”之類的格式內容,可能會干擾您的程序執行。 puts(str) (如果要添加 LF)或printf("%s", str)用於文本字符串再現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM