阅读与逗号分隔符的文件使用与fgets()和strtok的() [英] Reading the file with comma delimiter using fgets() and strtok()

查看:358
本文介绍了阅读与逗号分隔符的文件使用与fgets()和strtok的()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以逗号分隔的三个字段的文本文件。
我的文本文件的内容例如:12345,真正的编程新手,BS我
将文件加载到程序中,我用下面的code ....我的问题是,有时code ++工程,有时它没有(没有显示错误信息的程序只是关闭本身并没有按' ŧ继续)。我也观察到文本文件是空白的(没有什么是写的),它会自动关闭本身并不会继续。您的帮助将是非常美联社preciated。谢谢!

 内部阅读(){
     FILE *流= NULL;
     INT点击率;
     焦炭linebuffer [45];
     炭delims [] =;
     字符*号[3];
     字符*记号= NULL;
     流= FOPEN(student.txt,RT);
     如果(流== NULL)流=的fopen(student.txt,重量);
     其他{
          的printf(\\ nReading的学生名单目录等一下,请......);
          而(!的feof(流)){
                CTR = 0;
                与fgets(linebuffer,46,流);
                令牌= strtok的(linebuffer,delims);
                而(令牌!= NULL){
                  数[CTR] = linebuffer;
                  令牌= strtok的(NULL,delims);
                  CTR ++;
                }
          recordCtr ++;
          }
     recordCtr--;
     }
     FCLOSE(流);
}


解决方案

您绝不照搬标记一旦你发现了它。您不能复制 linebuffer ,因为在那里将被改写为下一行的数据加载。

这行:

 若干[CTR] = linebuffer;

应该引用标记保存最近发现的道理,但事实并非如此。它可能应该读的东西像 1

 的strcpy(编号[CTR],令牌);

但你不得不改变声明,确保有空间:

 字符数[3] [32];

显然,这引入了一个缓冲区溢出的风险,如果有一个很长的令牌,将不适合。如何最好地处理被留作练习。 :)

1 为什么临时矢量称为当它被用来存储两个数字和一个串(名称)号码是超越我

I have a text file with three fields separated by comma. Example of the content of my text file: 12345, true programming newbie, BS ME To load the file into the program, i used the below code.... my problem is that sometimes the code works and sometimes it doesn't (no error message appears the program just closes itself and doesn't continue). i also observed the text file is blank (nothing is written) it automatically closes itself and doesn't continue. Your help would be highly appreciated. Thanks!

int read(){
     FILE *stream = NULL;
     int ctr;
     char linebuffer[45];
     char delims[]=", ";
     char *number[3];
     char *token = NULL;
     stream = fopen("student.txt", "rt");
     if (stream == NULL) stream = fopen("student.txt", "wt");
     else {
          printf("\nReading the student list directory. Wait a moment please...");
          while(!feof(stream)){            
                ctr=0;
                fgets(linebuffer, 46, stream);
                token = strtok(linebuffer, delims);
                while(token != NULL){
                  number[ctr] = linebuffer;
                  token = strtok(NULL, delims); 
                  ctr++;
                }
          recordCtr++;                      
          }                     
     recordCtr--;
     }
     fclose(stream);
}    

解决方案

You never copy the token once you've found it. You can't copy linebuffer, as the data in there will be overwritten as the next line is loaded.

This line:

number[ctr] = linebuffer;

should reference token to save the most recently found token, but it doesn't. It should probably read something like1:

strcpy(number[ctr], token);

but then you'd have to change the declaration to make sure there's space:

char number[3][32];

Obviously, this introduces a buffer overrun risk, if there's a very long token it won't fit. How to best handle that is left as an exercise. :)

1 Why the temporary vector is called "number" when it is used to store two numbers and one string (the name) is beyond me.

这篇关于阅读与逗号分隔符的文件使用与fgets()和strtok的()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆