直到C ++中的文件结束 [英] reading until the end of file in C++

查看:77
本文介绍了直到C ++中的文件结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取,直到一个文件的电话簿应用程序,从C转换为C ++的结束。当我打印文件的结果,我得到这个:

I'm trying to read till the end of a file for a phonebook app that im converting from C to C++. When I print the the results from the file i get this:

johnny smith
(Home)3
(Cell)4
x☺> x☺>
(Home)4
(Cell)4

/ p>

it should print:

johnny smith
(Home)3
(Cell)4

现在我使用 while(!infile.eof()) which i 've read是一个糟糕的做法,但是当我使用 infile.getline()我得到一个重复的名字和姓氏,并且格式是全部。有没有(或者另一种方式)在输入结束时摆脱垃圾,或者另一种方式读取,直到C ++中的文件结束修复这个。我一直在阅读关于不同的解决方案,但很多网站似乎同意的是 fgets ,这是我用原来的C版本,但显然 fgets 不能使用 ifstream 这是我正在使用的。这里是代码:

Right now I'm using while(!infile.eof()) which i've read is a poor practice, but when I use infile.getline() I get a repeat of the first and last name, and the format is all jacked up. Is there anyway(or another way) to get rid of the junk at the end of the input or another way to read till the end of file in C++ that fixes this. I've been reading about different solutions, but the one a lot of sites seem to agree on is fgets, which is what I had with the original C version, but obviously fgets doesn't work with ifstream which is what I'm using. here is the code:

void contacts:: readfile(contacts*friends ,int* counter, int i,char buffer[],char    user_entry3[])
{
   ifstream read;
   read.open(user_entry3,ios::in);
   int len;
   contacts temp;
   *counter=0;
   i=0; 

     while (!read.eof()) { 
       temp.First_Name=(char*)malloc(36); 
       temp.Last_Name=(char*)malloc(36); 

       read>>temp.First_Name>>temp.Last_Name;

       read>>buffer;
       len=strlen(buffer);
       if(buffer[len-1]=='\n')
          buffer[len-1]='\0';

       temp.home=(char*)malloc(20); 
       strcpy(temp.home, buffer);

       read>>buffer;
       len=strlen(buffer);
       if(buffer[len-1]=='\n')
       buffer[len-1]='\0';


       temp.cell=(char*)malloc(20); 
       strcpy(temp.cell, buffer); 

      friends[i].First_Name=(char*)malloc(MAXNAME);
      friends[i].Last_Name=(char*)malloc(MAXNAME);
      friends[i].home=(char*)malloc(MAXPHONE);
      friends[i].cell=(char*)malloc(MAXPHONE);


  //adds file content to the structure
      strcpy(friends[*counter].First_Name,temp.First_Name);
      strcpy(friends[*counter].Last_Name,temp.Last_Name);
      strcpy(friends[*counter].home,temp.home);
      strcpy(friends[*counter].cell,temp.cell);


     (*counter)++;
     i++; 

   }
   //closes file and frees memory
    read.close();
    free(temp.Last_Name);
    free(temp.First_Name);
    free(temp.home);
    free(temp.cell);
}


推荐答案

/ strong>使用 eof()来确定是否到达文件结尾。而应阅读您要阅读的内容,然后然后检查您是否成功读取了数据。无法读取失败,您可以使用 eof()来确定错误是否已达到文件结尾,然后才生成格式错误的错误报告。

Do not use eof() to determine if you reached end of file. Instead, read what you want to read and then check if you successfully read the data. Obce reading failed you may use eof() to determine if the error is down to having reached the end of the file before producing an error report about a format error.

因为你提到你读过使用!infile.eof()是好的做法:你能指点我们的源的这个错误的信息?此信息需要更正。

Since you mentioned that you read that using !infile.eof() is good practice: Can you point us at the source of this wrong information? This information need correction.

这篇关于直到C ++中的文件结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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