简体   繁体   English

从输入文件读取并存储在数组C ++中

[英]Reading from input file and storing in array c++

I want to read from a file.txt that looks like this: 我想从看起来像这样的file.txt中读取:

process_id run_time process_id运行时间

T1 23 T1 23

T2 75 T2 75

Read each line and store integers of run time (tab separation)in an array 读取每一行并将运行时间的整数(制表符分隔)存储在数组中

My problem now is to read the content of the file .. and how to get the integer after the tab separation? 我现在的问题是读取文件的内容..以及在制表符分隔后如何获取整数?

thanks 谢谢

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main () 
{
int process_id[100];
int run_time[100];  
int arrival_time[100];
char quantum[50];
int switching;

char filename[50];
ifstream ManageFile; //object to open,read,write files
cout<< "Please enter your input file";
cin.getline(filename, 50);
ManageFile.open(filename); //open file using our file object

if(! ManageFile.is_open())
{
    cout<< "File does not exist! Please enter a valid path";
    cin.getline(filename, 50);
    ManageFile.open(filename);
}

while (!ManageFile.eof()) 
{
    ManageFile>>quantum;
    cout << quantum;

}

//ManageFile.close();
return 0;
}
  1. use C++, not C 使用C ++,而不是C
  2. don't use std::cin.getline, use std::getline (it works with std::string and is safer) 不要使用std :: cin.getline,请使用std :: getline(它与std :: string一起使用并且更安全)
  3. use a vector instead of hard-dimensioned arrays 使用向量而不是硬维数组
  4. use a vector of struct instead of "corresponding arrays" 使用结构的向量而不是“对应的数组”
  5. don't use while (!stream.eof()) 不要使用while (!stream.eof())

Here's a sample that might be helpful: 以下示例可能会有所帮助:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

struct Record {
    int process_id;
    int run_time;
    int arrival_time;
};

int main() {
    std::vector<Record> records;

    int switching;

    std::string filename;
    ifstream infile;

    while (!infile.is_open()) {
        cout << "Please enter your input file: ";
        std::getline(std::cin, filename);
        infile.open(filename); // open file using our file object

        cout << "File cannot be opened.\n";
    }

    std::string quantum;
    std::getline (infile, quantum); // skip header row

    while (std::getline(infile, quantum)) {
        // e.g.
        Record current;
        std::istringstream iss(quantum);
        if (iss >> current.process_id >> current.run_time >> current.arrival_time)
            records.push_back(current);
        else
            std::cout << "Invalid line ignored: '" << quantum << "'\n";
    }
}

Use function ignore from istream [ http://www.cplusplus.com/reference/istream/istream/ignore/] istream使用功能ignore [ http://www.cplusplus.com/reference/istream/istream/ignore/]

 while (!ManageFile.eof()) { std::string process_id; int run_time; ManageFile >> process_id; ManageFile.ignore (256, '\\t'); ManageFile >> run_time; } 

You can try something like this: 您可以尝试如下操作:

while (!ManageFile.eof())
{
    quantum[0] = 0;
    ManageFile>>quantum;
    if (strcmp(quantum, "0") == 0 || atoi(quantum) != 0)
        cout << quantum << endl;
}

Of course, you need to include in the head 当然,你需要包括在内

Using fscanf instead of ifstream can make the job a lot easier. 使用fscanf代替ifstream可以使工作轻松ifstream

char str[100];
int n;
....
fscanf(FILE * stream,"%s %d", str, &n);

You will get the string in str and integer in n. 您将在str获得字符串,在n中获得整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM