如何阅读从一个文本文件中的数据 [英] How to read data from a text file

查看:136
本文介绍了如何阅读从一个文本文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从我的文本文件中读取输入?输入文件是几行长,而且每行的格式为城市城市的距离,其中有两个城市以及它们之间的距离。

我已经试过几件事情要读取输入,但不幸的是那些没有工作。我需要
解析每行的各个值。 (每行包括2个城市名称和它们之间的距离。)任何帮助将是AP preciated。

 数据=的fopen(的argv [1],R);
而(!EOF){而(1){
    C =龟etc(数据);
    inname =(字符**)的malloc(sizeof的(字符*));
    如果(C =='')
        模式++;
    否则,如果(C =='\\ n'){模式= 0;
        打破;}
    其他{
        开关(模式3%){
            0的情况;
                对于(i = 0;龟etc(数据)='';!我++){
                    如果(inname [统计]!= NULL){算++; inname =(字符**)的malloc(sizeof的(字符*));}
                    inname [计数] [I] =龟etc(数据);}
                打破;
            情况1;
                如果(inname [统计]!= NULL){算++; inname =(字符**)的malloc(sizeof的(字符*));}
                对于(i = 0;龟etc(数据)='';!我++){
                    inname [计数] [I] =龟etc(数据);}
                打破;
            / * 2的情况下;对于(i = 0;龟etc(数据)='\\ n';!我++){
                    DIST [说] =的atoi(STR);}}} * /
                }}}算++;}
                `


解决方案

我想你应该考虑的的fscanf 读取格式化输入这个样子。

要读取包含两个字符串和一个int一条线,你会是这样的:

 的fscanf(数据,%s%S%D,&安培; city1,&安培;城2,与放;距离);

要读取多个行,直到EOF,你的code应为以下形式:

 而(的fscanf(数据,%s%S%D,&安培; city1,&安培;城2,和放大器;!距离)= EOF){
  / *你的逻辑在这里休息* /
}

How do I read input from my text file? The input file is several lines long, and each line is of the format city city distance where there are two cities and the distance between them.

I have tried several things to read the input, but unfortunately those did not work. I need to parse the individual values on each line. (Each line consists of 2 city names and the distance between them.) Any help would be appreciated.

data = fopen(argv[1],"r");
while(!EOF){

while(1){
    c=fgetc(data);
    inname=(char**)malloc(sizeof(char*));
    if(c==' ')
        mode++;
    else    if(c=='\n'){mode=0;
        break;}
    else {
        switch(mode%3){
            case 0;
                for(i=0;fgetc(data)!=' ';i++){  
                    if(inname[count]!=NULL) {count++;inname=(char**)malloc(sizeof(char*));}
                    inname[count][i]=fgetc(data);}
                break;
            case 1; 
                if(inname[count]!=NULL){ count++;inname=(char**)malloc(sizeof(char*));}
                for(i=0;fgetc(data)!=' ';i++){  
                    inname[count][i]=fgetc(data);}
                break;                                      
            /*case 2;for(i=0;fgetc(data)!='\n';i++){    
                    dist[say]=atoi(str);}}}*/
                }}}count++;}
                `

解决方案

I think you should look into fscanf for reading formatted input like this.

To read a line containing two strings and an int, you would have something like:

fscanf(data, "%s %s %d", &city1, &city2, &distance);

To read multiple lines until EOF, your code should be of the following form:

while(fscanf(data, "%s %s %d", &city1, &city2, &distance)!=EOF) {
  /* rest of your logic here */
}

这篇关于如何阅读从一个文本文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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