程序应读取文件名列表,打开这些文件并将其句柄放入结构数组中,然后读取字符串并使用结构数组中包含的句柄将连续的字符串行打印到最小文件中。我的程序将所有行中的数据仅放入一个文件,该文件最初是最小的,这是错误的,因为每次将数据打印到文件中时,它都应该是最小的文件。这是我的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
struct file_t
{
FILE* f;
int size;
}t[5];
void close_file(struct file_t* f) {
if (f == NULL || f->f == NULL) {
}
else {
fclose(f->f);
}
}
int open_file(struct file_t* f, const char* filename) {
if (f == NULL || filename == NULL) {
return 1;
}
FILE* fp;
fp = fopen(filename, "ab");
if (fp == NULL) {
return 2;
}
long int res = ftell(fp);
fclose(fp);
f->size = res;
f->f = fopen(filename, "ab+");
if (fp == NULL) {
return 2;
}
return 0;
}
struct file_t* find_min(const struct file_t* files, int size) {
if (files == NULL || size <= 0) {
return NULL;
}
int x = (files + 0)->size, i = 0, index = 0;
for (i = 0; i < size; i++) {
if ((files + i)->size <= x) {
x = (files + i)->size;
index = i;
}
}
return (struct file_t*)(files + index);
}
int main() {
puts("Input files' names:");
char tab[100];
int num = 0;
while(1==1){
if(fgets(tab, 100, stdin)==NULL||*tab=='\n'){
if (num == 0) {
printf("Couldn't open file");
return 4;
}
break;
}
int index=strlen(tab);
*(tab+index-1)='\x0';
if (strlen(tab) > 30) {
*(tab + 30) = '\x0';
}
if (open_file((t + num), tab) > 0) {
}
else {
num++;
}
}
if (num == 0) {
printf("Couldn't open file");
return 4;
}
char str[1000];
printf("Input text:");
*str = '\x0';
while (fgets(str, 1000, stdin)==NULL||*str!='\n') {
int index=strlen(str);
*(str+index-1)='\x0';
struct file_t* p = find_min(t, num);
fwrite(str, sizeof(char), strlen(str), p->f);
}
for (int i = 0; i < num; i++) {
close_file(t + i);
}
printf("File saved");
return 0;
}
写入文件时在哪里更新file_t->大小?您正在呼叫此:
fwrite(str,sizeof(char),strlen(str),p-> f);
但是之后,您应该执行p-> size + = strlen(str)来更新其大小,否则所有文件大小都将设置为初始值,因此所有字符串都将写入单个文件。
关于获取垃圾数据,请尝试在while循环中打印从scanf读取的字符串。
您正在使用scanf读取字符直到'\ n',但您并未读取'\ n'本身。您还需要在该循环中使用fseek(stdin, 0, SEEK_END);
。
最后,为什么要使用这样的语法:
(文件+ i)->大小
当您可以这样更清晰地调用它时:
files[i].size
因此,您的代码真的很难阅读。
您需要发布一些严重的错误。
fseek(stdin, 0, SEEK_END)
-fseek
通常仅在磁盘文件或类似的文件上工作。请参考此链接Using fseek with a file pointer that points to stdin事实上,即使fflush()
也不起作用。 fflush
是专为刷新输出流及其与输入流的行为相关的实现而设计的。请参阅此链接以获取更多详细信息stdinflush
scanf("%[^\n]s", tab)
如果您在循环中多次使用它,则只有第一次读取会成功。原因是,\n
字符已从先前的输入中删除,并且如前所述,fflush()
可能无法成功删除该\n
。对scanf()
的进一步调用将仅返回而无需读取任何内容。
'\0x'
如果打算将其用作字符串终止符,则不是。它是一个具有整数值120
的多字符常量。下面是模糊的测试运行代码
#include <stdio.h>
int main()
{
if ('\0' == '\0x' )
printf("both are same\n");
printf("%d",'\0x');
}
编译警告
test.c: In function ‘main’:
test.c:5:14: warning: multi-character character constant [-Wmultichar]
5 | if ('\0' == '\0x' )
| ^~~~~
test.c:8:14: warning: multi-character character constant [-Wmultichar]
8 | printf("%d",'\0x');
| ^~~~~
输出
120
fseek(fp, 0, SEEK_END); ftell(fp);
-不应用于确定文件大小。对于二进制文件,fseek()
的行为不确定。请参考此链接Do not use fseek() and ftell() to compute the size of a regular file
某些逻辑错误
1]每当将数据写入文件时,文件大小都会更改,因此每次都要在find_min()
中计算文件大小。
2)fwrite()
实际上不会立即将数据转储到文件中。您需要致电fflush()
。
解决上述问题后,这是修改后的代码。
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <sys/stat.h>
struct file_t
{
FILE* f;
int size;
}t[5];
void close_file(struct file_t* f) {
if (f == NULL || f->f == NULL) {
}
else {
fclose(f->f);
}
}
int open_file(struct file_t* f, const char* filename) {
if (f == NULL || filename == NULL) {
return 1;
}
f->f = fopen(filename, "a");
if (f->f == NULL)
return 2;
struct stat statbuf;
fstat(fileno(f->f), &statbuf);
f->size = statbuf.st_size;
return 0;
}
struct file_t* find_min(const struct file_t* files, int size) {
if (files == NULL || size <= 0) {
return NULL;
}
struct stat statbuf;
fstat(fileno(files->f), &statbuf);
int x = statbuf.st_size, i = 0, index = 0;
for (i = 0; i < size; i++) {
fstat(fileno((files+i)->f), &statbuf);
if (statbuf.st_size < x) {
x = statbuf.st_size;
index = i;
}
}
return (struct file_t*)(files + index);
}
int main() {
puts("Input files' names:");
char tab[100];
int num = 0;
while(1){
int c;
while (1) {
c = getc(stdin);
if (c == EOF || c == ' ')
goto user_input;
if(c != '\n')
break;
}
tab[0] = c;
if (scanf("%[^\n]s", tab+1) == EOF)
break;
if (*tab == '\0') {
if (num == 0) {
printf("Couldn't open file");
return 4;
}
break;
}
if (strlen(tab) > 30) {
*(tab + 30) = '\0';
}
if (open_file((t + num), tab) > 0) {
}
else {
num++;
}
*tab = '\0';
}
user_input:
if (num == 0) {
printf("Couldn't open file");
return 4;
}
fflush(stdin);
char str[1000];
printf("Input text:\n");
*str = '\0';
while(1) {
int c;
while(1) {
c = getc(stdin);
if (c == EOF)
goto main_exit;
if (c != '\n')
break;
}
str[0] = c;
if (scanf("%[^\n]s", str+1) == EOF)
break;
struct file_t* p = find_min(t, num);
fwrite(str, sizeof(char), strlen(str), p->f);
fflush(p->f);
}
main_exit:
for (int i = 0; i < num; i++) {
close_file(t + i);
}
printf("File saved");
return 0;
}
总会
$ ./a.out
Input files' names:
test file1.txt
test file2.txt
' '(NOTE: Space character inputted before pressing enter.)
Input text:
this is
stackoverflow
File saved
test file1.txt
this is
test file2.txt
stackoverflow
注意从第一个循环(文件输入)终止。您需要输入空格,然后按Enter(您可以对此进行调整)。