C++ programming
Write a program that reads a comma-separated file (CSV) with integer values and prints the number of integer values in the file. Your program's input will be a string with the name of the file. If the file does not exist, then the program must print: Error opening the file
For example, given the following CSV file input1.csv:
1,10,20,30,40
The output of your program must be:
5
You can safely assume that the input file will be a valid CSV file.
Below is my code but I think I miss something which makes the program does not run. can someone give me the correct code? Thank you
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int input1;
ifstream inFS;
conut = 0;
inFS.open("input1.csv");
while (!inFS.eof()){
inFS >> input1;
if (!inFS.fail()){
cout << count(input1) << endl;
}
}
inFS.close();
return 0;
}
#include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main() { ifstream inFS; int num, count = 0; string filename, s; cout << "Enter file name: "; cin >> filename; inFS.open(filename.c_str()); if (inFS.is_open()) { while (getline(inFS, s, ',')) { num = atoi(s.c_str()); cout << num << endl; ++count; } cout << "Number of values in file is " << count << endl; inFS.close(); } else { cout << filename << " does not exists!" << endl; } return 0; }
Get Answers For Free
Most questions answered within 1 hours.