程序应该读取文件名列表,打开这些文件并把它们的句柄放在结构数组中,然后读取字符串并利用结构数组中的句柄把连续的字符串行打印到最小的文件中。
我的程序只把所有行的数据放到一个最初最小的文件中,这是假的,因为每次打印数据到文件中时,应该是最小的文件。这是我的程序。
#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;
}
有一些关键的错误需要你解决。
fseek(stdin, 0, SEEK_END)
-- fseek
通常只能在磁盘文件上工作,或其他类似的东西。请参考这个链接 使用fseek,文件指针指向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()
与 SEEK_END
在二进制文件的情况下是不确定的。请参考这个链接 不要使用fseek()和ftell()来计算常规文件的大小。
一些逻辑错误
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
测试文件1.txt
this is
测试文件2.txt
stackoverflow
注意从第一个循环(文件输入)中断。你需要输入空格,然后按回车键(你可以围绕这个进行调整)。
当你把文件写入一个文件时,你在哪里更新file_t->大小呢?
fwrite(str, sizeof(char), strlen(str), p->f);
但之后你应该做p->size += strlen(str)来更新它的大小,否则所有的文件大小都被设置为初始值,因此所有的字符串都会被写入一个文件。
至于得到垃圾数据,可以尝试在while循环中打印你从scanf读取的字符串。
你使用scanf读取字符直到'\n',但你没有读取'\n'本身。你需要一个 fseek(stdin, 0, SEEK_END);
在该循环中也是如此。
最后,为什么你使用这样的语法。
(files + i)->size
当你能像这样更简洁地调用它。
files[i].size
你的代码真的很难读,因为这个。