69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *file = fopen("a.txt", "r");
if (file == NULL) return 0;
fseek(file, 0L, SEEK_END);
int fileLength = ftell(file); //此处读出为34,User Login长度10,第一第二2行的/r/n即回车换行总共4.第三行没有换行回车。
printf("fileLength= %d\n", fileLength); //读出的是36,而不是34
if (fileLength == 0) return 0;
fseek(file, 0L, SEEK_SET);
char buf[1024];
fileLength = fread(buf, sizeof(char), 1024, file);
buf[fileLength] = '\0';
printf("fileLength= %d\n %s", fileLength, buf);
return 1;
}
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
FILE *file = fopen("1.txt", "r");
if (file == NULL) return 0;
fseek(file, 0L, SEEK_END);
int fileLength = ftell(file); //此处读出为34,User Login长度10,第一第二2行的/r/n即回车换行总共4.第三行没有换行回车。
if (fileLength == 0) return 0;
fseek(file, 0L, SEEK_SET);
char buf[1024];
fread(buf, sizeof(char), fileLength, file);
buf[fileLength-1] = '\0'; //这里修改,溢出了数组范围了
printf("%s", buf);
system("pause");
return 0;
}