c语言删除文本文件空白行,关于文件操作,碰到空格就换行

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

int main()

{

int i, off_set = 0;

char file_to_open[81];

char lines[1024], new_string[102400];

FILE *fp_read, *fp_write;

memset(file_to_open, 0x00, sizeof(file_to_open));

printf("Please input the files' full path:\n");

gets(file_to_open);

fp_read = fopen(file_to_open, "r");

if(NULL == fp_read)

{

printf("You've input a wrong path!\nProgramme will exit...\n");

sleep(3);

exit(1);

}

memset(new_string, 0x00, sizeof(new_string));

while(!feof(fp_read))

{

static int space_count = 0;

memset(lines, 0x00, sizeof(lines));

if(!fgets(lines, 1024, fp_read))

break;

printf("Now !");

for(i = 0; i < strlen(lines); i++)

{

if(lines[i] == 0x20)

{

lines[i] = '\n';

space_count ++;

}

}

strncpy(new_string + off_set, lines, strlen(lines));

off_set += strlen(lines);

}

printf(new_string);

fp_write = fopen("new_txt.txt", "w");

if(NULL == fp_write)

{

printf("Failed to open the new file!\n");

exit(1);

}

fprintf(fp_write, "%s", new_string);

printf("新文件写入成功!\n");

return 0;

}

linux gcc 编译成功并已测试,没暂时没发现bug。有问题继续问。

如果你是在windows下跑这个代码遇到错误,可能是中间有个sleep函数,去掉便可。

### 如何在C语言中读取带有空格的TXT文件 在C语言中,可以通过多种方式来处理带空格文本文件。以下是几种常见的方法及其适用场景。 #### 方法一:使用`fscanf`函数逐字段读取 当需要按空格分割数据并分别存储时,可以利用`fscanf`函数逐一提取字段。此方法适用于简单的结构化数据输入[^1]。 ```c #include <stdio.h> int main() { int data; FILE *fp = fopen("in.txt", "r"); if (!fp) { printf("Can't open file\n"); return -1; } while (fscanf(fp, "%d", &data) != EOF) { printf("%d ", data); // 输出读取的数据 } fclose(fp); return 0; } ``` 上述代码通过指定格式符`%d`跳过空白字符(包括空格、制表符和换行),从而仅保留数值部分。 --- #### 方法二:使用`fgets`配合字符串解析 对于更复杂的场景,尤其是涉及多个连续空格或其他特殊分隔符的情况,推荐先整体读取整行内容再进行拆解。这种方法更加灵活且易于控制[^2]。 ```c #include <stdio.h> #include <string.h> #include <ctype.h> #define MAX_LINE_LEN 256 void trim_whitespace(char* str) { char *end; // 删除前导空格 while (isspace((unsigned char)*str)) str++; if (*str == '\0') return; // 找到最后一个非空字符的位置 end = str + strlen(str) - 1; while (end >= str && isspace((unsigned char)*end)) end--; *(end + 1) = '\0'; } int main() { FILE *fp = fopen("in.txt", "r"); if (!fp) { printf("Can't open file\n"); return -1; } char line[MAX_LINE_LEN]; while (fgets(line, sizeof(line), fp)) { trim_whitespace(line); // 移除首尾多余空格 printf("[%s]\n", line); // 显示清理后的每行内容 } fclose(fp); return 0; } ``` 该方案首先调用`fgets`一次性获取一行文字,随后借助自定义辅助函数去除冗余空白区域。 --- #### 方法三:基于单词级别的精确匹配 如果目标是从文档里抽取独立词语而非固定列数,则可考虑采用循环机制逐步推进指针位置直至找到下一个有效标记为止[^3]。 ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> char* get_next_word(FILE* stream); int main() { FILE *fp = fopen("in.txt", "r"); if (!fp) { printf("File not found.\n"); return EXIT_FAILURE; } char buffer[BUFSIZ], word[80]; while ((word = get_next_word(fp))) { strcpy(buffer, word); free(word); puts(buffer); } fclose(fp); return EXIT_SUCCESS; } char* get_next_word(FILE* stream) { static char buf[BUFSIZ]; /* 缓冲区 */ size_t i = 0; /* 当前索引 */ do { int c = fgetc(stream); if (isalpha(c) || isdigit(c)) { /* 如果当前字符属于字母或数字范围内的合法成分 */ buf[i++] = tolower(c); /* 转换成小写字母形式存入临时缓冲区内备用 */ } else if (i > 0) break; /* 遇见非法符号并且已经累积了一定长度则停止收集过程进入返回阶段 */ } while (!feof(stream)); if (i == 0) return NULL; /* 若无任何实际成果产出即刻退出整个流程宣告失败告终 */ buf[i] = '\0'; /* 添加终止标志完成最终组装工作形成标准字符串对象准备交付给上层调用者享用 */ return strdup(buf); /* 动态分配内存空间复制原始版本确保安全可靠传递出去供外部自由处置不受内部状态影响干扰 */ } ``` 这段程序实现了动态识别任意数量间距下的单独词汇功能,并妥善管理资源释放环节防止泄露隐患发生。 --- #### 总结说明 以上三种策略各有优劣之处,在具体实践中需依据实际情况选取最合适的解决方案: - **简单分离型**适合快速筛选纯量值集合; - **全盘掌控型**能够应对复杂布局需求提供更高精度调整选项; - **精细剖析型**专注于挖掘隐藏于大量杂乱信息中的核心片段展现卓越洞察力价值所在。 无论采取哪种途径都务必注意边界条件判断以及异常情况防范措施落实到位以免引发不可预期后果损害系统稳定性表现水平下降等问题出现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值