|
沙发
楼主 |
发表于 2021-5-6 11:45:59
|
只看该作者
修改一个bug:
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #define BUFFER_SIZE 15
- #define SAMPLE_NUM 768
- //C中一行一行的读入文件的字符串
- void main(){
- FILE *pf;
- char buffer[BUFFER_SIZE];
- long int value=0;
- int str_len=0;
- double one_wave_ru[SAMPLE_NUM]={0};
- int counter = 0;
- //char file_path[50] = "F:\\TWR\\ir_uwb_ori_data\\一个动目标_过道_1_0329.txt";
- char file_path[50] = "F:\\TWR\\ir_uwb_ori_data\\test_5_wave.txt";
-
- if((pf = fopen(file_path , "r")) == NULL)
- {
- printf("打开文件失败!\n");
- exit(EXIT_FAILURE);
- /*
- 头文件 <stdlib> 中宏定义如下:
- #define EXIT_FAILURE 1 表示程序执行失败
- #define EXIT_SUCCESS 0 表示程序执行成功
- */
- }
-
-
- while(!feof(pf)) //检测到文件末尾,返回非 0 值,否则返回 0 值
- {
- fgets(buffer , BUFFER_SIZE , pf);
- /*
- 最多可读取 BUFFER_SIZE - 1 个字符
- 因为结尾处会自动添加一个 '\0' 字符串结束符
- 当读取到换行符 '\n' 和文件结束符 EOF 时结束读取
- 注意:'\n' 会被作为一个合法的字符读取
- 注意:1.txt 文件中有中文,需要将其编码格式保存为 ANSI ,即当前系统编码 GBK 编码
- 注意:1.txt 文件中最后一行有换行符存在,则最后一行将重复输出一次
- */
-
- /*
- 输出读取到的 '\n' 字符,即会自动换行
- */
- str_len = strlen(buffer);
- if (str_len == 0)
- {
- printf("空字符串:本身就是 一定在末尾 break出A\n");
- break;
- }
- buffer[str_len-1] = '\0';
- str_len = strlen(buffer);
- if (str_len == 0)
- {
- printf("空字符串:原本有一个回车换行 的 可能在中间 break出B\n");
- //continue; // 不容许这样 怕乱套
- break;
- }
- //字符串转数字
- value = atoi(buffer);
- //整形转double吧
-
- one_wave_ru[counter%SAMPLE_NUM] = value;
- printf("已放到数组的索引=%d;", counter%SAMPLE_NUM);
- if (counter%SAMPLE_NUM == (SAMPLE_NUM-1))
- {
- printf("------------------------------放满一个回波数据了,才能入队列\n");
- }
- counter++; // 会不会乱套?
- //返回转换后的整型数;如果 str 不能转换成 int 或者 str 为空字符串,那么将返回 0
- printf("计数器=%d;%s与其长度=%d;转数字=%d\n" , counter, buffer, str_len, value);
- //字符串 数组 清空 不清空最后会多一行
- memset(buffer, 0, sizeof(buffer));
-
- }
- fclose(pf);
- system("pause");
- return ;
- }
- /************************************************************************/
- /* 【函数说明】atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),
- 直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。 */
- /************************************************************************/
复制代码 |
|