2.12 LAB 2.3: File I/O - CSV update
This program should
get names of input and output files from command line (NOT from user input)
read in integers from a csv (comma-separated values) file into a vector
compute the integer average of all of the values
convert each value in the vector to the difference between the original value and the average
write the new values into a csv file
Working code implemented in C++ and appropriate comments provided for better understanding:
Source code for main.cpp:
#include
#include
#include
using namespace std;
int main(int argc, char *argv[]) {
string inputFile;
string outputFile;
if (argc != 3) {
cout << "Usage: progname inputFile outputFile"
<< endl;
return 1;
}
// Assign to inputFile value of 2nd command line argument
inputFile = argv[1];
// Assign to outputFile value of 3rd command line argument
outputFile = argv[2];
// Create input stream and open input csv file.
ifstream inFS(inputFile);
// Verify file opened correctly.
// Output error message and return 1 if file stream did not open
correctly.
if (!inFS.is_open()) {
cout << "Error opening " << inputFile
<< endl;
return 1;
}
// Read in integers from input file to vector.
vector
string inputString;
int inputInt;
bool done = false;
while (!done) {
inFS >> inputInt;
intVector.push_back(inputInt);
if (!inFS.eof()) { // After grabbing int, check for
more
inFS.clear(); // Clear the error
state
char dummyChar;
inFS >> dummyChar; // Take
the comma
}
else
{
done = true; // we've reached the
end, so we're done
}
}
// Close input stream.
inFS.close();
// Get integer average of all values read in.
int average;
int total = 0;
for (unsigned i = 0; i < intVector.size(); ++i) {
total = total + intVector.at(i);
}
average = total / intVector.size();
// Convert each value within vector to be the difference between
the original value and the average.
for (unsigned i = 0; i < intVector.size(); ++i) {
intVector.at(i) = intVector.at(i) - average;
}
// Create output stream and open/create output csv file.
ofstream outFS(outputFile);
// Verify file opened or was created correctly.
// Output error message and return 1 if file stream did not open
correctly.
if (!outFS.is_open()) {
cout << "Error opening " << outputFile
<< endl;
return 1;
}
// Write converted values into ouptut csv file, each integer
separated by a comma.
for (unsigned i = 0; i < intVector.size(); ++i) {
outFS << intVector.at(i);
if (i + 1 < intVector.size()) { // if there's more
ints to write, put another comma
outFS << ',';
}
}
// Close output stream.
outFS.close();
return 0;
}
Sample Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.
get names of input and output files from command line (NOT from user input)
Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...
JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...
Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...
Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...
This is a standard C++ programming assignment using a command line g++ compiler or the embedded one in zyBooks. No GUI forms and no Visual Studio. No external files and no databases are used. There is only one object-oriented program to complete in this assignment. All code should be saved in a file named ExamScoresUpdate.cpp, which is the only file to submit for grading. No .h files used. The .cpp file contains main() and two classes, ExamScores and DataCollector. There...
2. Write a program to read two lists of names from two input files and then match the names in the two lists using Co-sequential Match based on a single loop. Output the names common to both the lists to an output file, In Java
write a function: Names(path): that reads from a file at location path and returns a dictionary mapping from column names to lists containing the data in those columns. The format of the file is a csv file. The first line is a comma separated set of string names for the columns contained in the file. The function will accumulate a dictionary mapping from year to the list of years, from name to the list of names (in the same order),...
UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how this fundamentally works at the system call level to understand higher-level system programming concepts. Every program automatically has three file descriptors opened by the shell standard input standard output standard error 1 2 One can use read and write other open file. Normally, standard input and output on the terminal are line-buffered, so, for example, the specified number of...
Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1. The input and variable value file are both text files that will be specified in...
Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method of search either binary or linear, creates a file of the number of books found from the request list. program should receive two filenames as command line arguments, the new books file and request file, which the program should read in. The list of new books and list of requested books should should be stored in only two std::vector containers. Because some code to...