C++ c++;从文本文件获取数组字符串

C++ c++;从文本文件获取数组字符串,c++,text-files,C++,Text Files,我的文本文件就像 Jason Derulo 91 Western Road,xxxx,xxxx 1000 david beckham 91 Western Road,xxxx,xxxx 1000 我试图从文本文件中获取数据并将其保存到数组中,但是当我想将文本文件中的数据存储到数组中时,它会不停地循环。我该怎么办?循环中存在的问题或从文本文件获取数据的方法 代码: #包括 #包括 使用名称空间std; 类型定义结构{ 字符名[30]; 字符地址[50]; 双平衡; }账户; //功能原型 无效菜

我的文本文件就像

Jason Derulo
91 Western Road,xxxx,xxxx
1000
david beckham
91 Western Road,xxxx,xxxx
1000
我试图从文本文件中获取数据并将其保存到数组中,但是当我想将文本文件中的数据存储到数组中时,它会不停地循环。我该怎么办?循环中存在的问题或从文本文件获取数据的方法

代码:

#包括
#包括
使用名称空间std;
类型定义结构{
字符名[30];
字符地址[50];
双平衡;
}账户;
//功能原型
无效菜单();
作废读取数据(账户记录[]);
int main(){
帐户记录[31];//定义最大大小为30的数组“记录”
读取数据(记录);
}
//--------------------------------------------------------------------
作废读取数据(账户记录[]){
ifstream openfile(“list.txt”);//打开文本文件
如果(!openfile){

不能使用
ifstream::getline()
而不是
ifstream::eof()
>
一起使用。以下是一个示例(为简单起见,我没有检查流是否正确打开)

#包括
#包括
#包括
使用名称空间std;
#定义ARR_尺寸31
类型定义结构{
字符名[30];
字符地址[50];
双平衡;
}账户;
int main(){
账户温度,记录[ARR_SIZE];
ifstream ifile(“list.txt”);
int i=0;
双d=0;
而(i>d).get(){temp.balance=d;}
记录[i]=温度;
i++;
}
对于(int i=0;icout
loop
-1
,你可以访问
record[loop]
?你的
void
函数试图返回一个
int
。另外,不要这样做。非常感谢!!!我明白了..但是你为什么要使用.get()来实现“平衡”,我们不能直接使用ifile>吗?@noobies:我们使用
.get()
,因为
getline()
不会从流中删除
'\n'
.get()
会为我们这样做。有关详细信息,请参阅引用的链接。
#include <iostream>
#include <fstream>

using namespace std;

typedef struct {

    char name[30];
    char address[50];
    double balance;

} ACCOUNT;

//function prototype
void menu();
void read_data(ACCOUNT record[]);

int main() {
    ACCOUNT record[31]; //Define array 'record'  which have maximum size of 30
    read_data(record);  
}
//--------------------------------------------------------------------

void read_data(ACCOUNT record[]) {
    ifstream openfile("list.txt");              //open text file 

    if (!openfile) {
        cout << "Error opening input file\n";
        return 0;
    } else {
        int loop = -1;                  //size of array 
        cout << "--------------Data From File--------------"<<endl;
        while (!openfile.eof())  {
        if (openfile.peek() == '\n') 
            openfile.ignore(256, '\n');
        openfile.getline(record[loop].name, 30);
        openfile.getline(record[loop].address, 50);
        openfile >> record[loop].balance;
        }
        openfile.close();               //close text file

        for (int i = 0; i <= loop + 1; i++) {
            cout << "Account "  << endl;
            cout << "Name         : " << record[i].name << endl;
            cout << "Address      : " << record[i].address << endl;
            cout << "Balance      : " << record[i].balance << endl;
        }
    }
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#define ARR_SIZE 31

typedef struct {
    char name[30];
    char address[50];
    double balance;
} ACCOUNT;

int main() {
  ACCOUNT temp, record[ARR_SIZE];
  ifstream ifile("list.txt");
  int i=0;
  double d=0;

  while(i < ARR_SIZE) {
    ifile.getline(temp.name, 30, '\n');//use the size of the array
    ifile.getline(temp.address, 50, '\n');//same here

    //consume the newline still in the stream:    
    if((ifile >> d).get()) { temp.balance = d; }

    record[i] = temp;
    i++;
  }

  for (int i=0; i < ARR_SIZE; i++) {
    cout << record[i].name << "\n" 
         << record[i].address << "\n" 
         << record[i].balance << "\n\n";
  }
  return 0;
}