I want to get multiple lines sequentially from a file and then save them to a variable. If in Java can be using scanner.nextInt.
How about C++?
int main(){
string line;
int a, b, c;
ifstream myFile("input.in");
if(myFile.is_open()){
while(getline(myFile,line)){
int cases = atoi(line.c_str());
double count[cases];
cout << "cases : "<<cases << "\n";
for(int i = 1; i <= cases; i++){
a = atoi(line.c_str());
b = atoi(line.c_str());
c = atoi(line.c_str());
cout << a << b << c;
}
}
}
return 0;
}
You can use while(input.in >> cases) to read the next int into cases until end of file (EOF) is reached. I've updated your code below.
int main(){
string line;
int a, b, c;
ifstream myFile("input.in");
if(myFile.is_open()) {
int cases = 0;
while(myFile >> cases) { // breaks on eof
double count[cases];
cout << "cases : " << cases << "\n";
for(int i = 1; i <= cases; i++){
myFile >> a;
myFile >> b;
myFile >> c;
cout << a << b << c;
}
}
}
return 0;
}
Related
I'm learning how to use fstream with dynamic array, the program supposed to print out all the data in txt file and can do something like sorting, delete gender or calculate average grade. But turned out code is all messy and arrays are hard to access.
This is the text file use to practice.
Code:
struct studentData{
string branch;
string name;
char gender;
double grade;
int number;
int total;
};
int main() {
int line_count = 0;
ifstream file_in;
int couter = 0;
file_in.open("student.txt");
if(!file_in.good())
{
cout << "Eror, could not open the file." << endl;
file_in.clear();
return -1;
}
line_count = openFileTest(file_in);
file_in.clear();
file_in.seekg(ios::beg);
studentData* p_studentData = new studentData[line_count];
loadStudentData(file_in, p_studentData,couter);
displayStudentData(p_studentData, line_count,couter);
delete [] p_studentData;
file_in.close();
return 0;
}
int openFileTest(ifstream& file_in)
{
string temp;
int linecount = 0;
while(getline(file_in, temp))
{
linecount ++;
}
return linecount;
}
void loadStudentData(ifstream& file_in,studentData* p_studentData,int couter)
{
int temp ;
file_in >> p_studentData -> total ;
temp = p_studentData->total;
for(int i=0;i<temp;i++)
{
file_in >> p_studentData->branch >> p_studentData->number ;
for(int k=0;k<p_studentData[i].number;k++)
{
file_in >> p_studentData[k].name >> p_studentData[k].gender >> p_studentData[k].grade ;
}
p_studentData++;
}
return;
}
void displayStudentData(studentData* p_studentData, int count_line,int couter)
{
cout << p_studentData->total << endl;
int temp = p_studentData->total ;
for(int i=0;i<temp;i++)
{
cout << p_studentData[i].branch << " " ;
cout << p_studentData[i].number << endl;
for(int j=0;j<p_studentData[i].number;j++)
{
cout << p_studentData[j].name << " " << p_studentData[j].gender << " " << p_studentData[j].grade << endl;
}
}
return;
}
The output I get is:
It appears you are not being careful to track where you are in the file. I don't know why arrays[] are so popular for these kinds of assignments when a vector would do, but notice that you are missing a level of abstraction:
your code: array of student
assignment: array of subject; each subject is an array of student
It is often very helpful to use types to keep track of this:
struct student
{
// as above
};
struct subject
{
string name;
int number_of_students;
student* students;
};
struct all_subjects
{
int number_of_subjects;
subject* subjects;
};
Using this, we can work on deciphering the file:
all_subjects load_all_subjects()
{
ifstream f( ... );
...
all_subjects all;
f >> all.number_of_subjects;
all.subjects = new subject[ all.number_of_subjects ];
for (int n = 0; n < all.number_of_subjects; n++)
load_one_subject( f, all.subjects[ n ] );
return all_students;
}
void load_one_subject( std::ifstream& f, subject& one_subject )
{
f >> one_subject.name;
f >> one_subject.number_of_students;
one_subject.students = new student[ one_subject.number_of_students ];
for (int n = 0; n < one_subject.number_of_students; n++)
load_one_student( f, one_subject.students[ n ] );
}
And so on. Good luck!
I'm trying to write a code that will get values from a file and store them into a class variable. I'm not saving them according to my code. I would really appreciate the help. I don't know where my error is. The first value from the file is the number of class objects there is in total.
class customer {
private:
string *names;
char *plans;
int *hours;
int customerTotal;
public:
string name;
char plan;
int hour;
customer() {
name = "";
plan = ' ';
hour = 0;
customerTotal = 0;
}
void setName(string x) {
names[customerTotal] = x;
}
void setPlan(char x) {
plans[customerTotal] = x;
}
void setHours(int x){
hours[customerTotal] = x;
customerTotal++;
}
void printArray(){
for (int i = 0; i < customerTotal;i++){
cout << names[i] << plans[i] << hours[i] << endl;
}
}
};
int main() {
int numCustomers;
ifstream inFile;
string n;
char p;
int h;
inFile.open("customers.txt");
if (!inFile)
{
cout << "File not found!" << endl << endl;
system("pause");
return 1;
}
customer x;
inFile >> numCustomers;
while (!(inFile.eof())) {
inFile >> n >> p >> h;
x.setName(n);
x.setPlan(p);
x.setHours(h);
}
x.printArray();
inFile.close();
}
program should read numbers from data.txt file and then print them, but prints only first value good, all other values are the same -9.25596e+061
const char CDfv[] = "Data.txt";
const int cmax = 1000;
//---------------------------------------------------------
void read_print_data (double A[], int& x);
//--------------------------------------------------------
int main()
{
ofstream fr;
double A[cmax];
int x;
ifstream fv ("Data.txt");
read_print_data(A,x);
system("Pause");
return 0;
}
void read_print_data (double A[], int& x)
{
ifstream fd(CDfv);
fd >> x;
for (int i = 0; i < x; i++)
{
fd >> A[i];
cout << i + 1 << " " << A[i] << endl;
fd.close();
}
}
You are closing the stream prematurely, You need to move
fd.close();
to be outside the loop.
So the code should look like
for (int i = 0; i < x; i++)
{
if (fd >> A[i]) {
cout << i + 1 << " " << A[i] << endl;
} else {
// Error has occurred
}
}
fd.close();
I want to ask about my code below: (The code below basically read an input file named inputVelocity.dat with format stated in the code. The code reads with istringstream to pass each value to each particular arrays)
std::ifstream inputVelocity("input/inputVelocity.dat");
std::string lineInputVelocity;
while (std::getline(inputVelocity, lineInputVelocity)) {
std::istringstream issVelocity(lineInputVelocity);
double a, b, c, d, e;
if (!(issVelocity >> a >> b >> c >> d >> e)) {
std::cout << "ISS ERROR" << std::endl;
}
for (int k=0; k<=nz+1; k++) {
for (int j=0; j<=ny+1; j++) {
for (int i=0; i<=nx+1; i++) {
ux[i][j][k] = a;
uy[i][j][k] = b;
uz[i][j][k] = c;
pressure[i][j][k] = d;
temperature[i][j][k] = e;
}
}
}
}
inputVelocity.close();
The code is fine when reading around 20000 lines, but when I change the file into around 1.6 million lines, the code run very slow even on a server.
I did std::cout on each getline loop and it read like 5 lines/second, with around 1.6 million lines.
I have found some related questions here but still can't understand what's the problem source and how to fix it. Anyone can help?
Thank you.
I changed the code into this:
std::ifstream inputVelocity("input/inputVelocity.dat");
std::string lineInputVelocity;
int getI, getJ, getK;
getI = 0;
getJ = 0;
getK = 0;
while (std::getline(inputVelocity, lineInputVelocity)) {
std::istringstream issVelocity(lineInputVelocity);
double a, b, c, d, e;
if (!(issVelocity >> a >> b >> c >> d >> e)) {
std::cout << "ISS ERROR" << std::endl;
}
std::cout << getI << " " << getJ << " " << getK << std::endl;
ux[getI][getJ][getK] = a;
uy[getI][getJ][getK] = b;
uz[getI][getJ][getK] = c;
pressure[getI][getJ][getK] = d;
temperature[getI][getJ][getK] = e;
getK = getK + 1;
if (getK == nz+2) {
getJ = getJ + 1;
getK = 0;
}
if (getJ == ny+2) {
getI = getI + 1;
getJ = 0;
}
}
inputVelocity.close();
And that worked really well :)
If anyone has a more efficient solution, I would be very glad to see! :)
AT
4
5
6
7
#include<iostream>
#include<stdio.h>
#include <fstream>
using namespace std;
int main()
{
int data[4], a, b, c, d, e, f;
ifstream myfile;
myfile.open("tera.txt");
for (int i = 0; i < 4; i++)
{
myfile >> data[i];
}
myfile.close();
a = data[0];
b = data[1];
c = data[2];
d = data[3];
cout << a << "\t" << b << "\t" << c << "\t" << d << "\n";
return 0;
}
it takes AT also and give garbage value. how and where should i use ignore function to ignore AT Value.
And there is one thing more if there is another array given BT containing some value like this:
AT BT
how to store BT's all values under it in an array?
You just have to skip the first line. You can also add optional error handling, otherwise read may fail for all line.
if (!myfile)
{
cout << "can't open\n";
return 0;
}
string temp;
myfile >> temp;
cout << "first line: " << temp << endl;
for (int i = 0; i < 4; i++)
{
myfile >> data[i];
if (myfile.fail())
{
cout << "error\n";
myfile.clear();
myfile.ignore(1000000, '\n');
}
}