how to make input prompt for 7 column by ifstream - c++

I typed in a file name which I want to show on prompt screen but it says that
"'c:\test\sp.csv' is not recognized as an internal or external command,"
even though the file is available on the path.
1 - Why did this error happen? How to fix it?
C:\Users\MS>c:\test\sp.csv
'c:\test\sp.csv' is not recognized as an internal or external command,
operable program or batch file.
2 - The code below only shows one column, if I want to input for 7 columns, how would I edit the code below?
How to print out on prompt screen using ifstream for 7 column with header and price.
Date Open High Low Close Volume Adj Close
6/21/2013 1588.62 1599.19 1577.7 1592.43 5797280000 1592.43
6/20/2013 1624.62 1624.62 1584.32 1588.19 4858850000 1588.19
int main(){
int open;
string fileName;
cout <<"Enter a file name: ";
getline(cin, fileName); //c:\\test\\sp.csv
ifstream inFile(fileName.c_str(), ios::in);
while(!inFile.eof()){
inFile >> open;
cout << open << endl;
}
inFile.close();
system("PAUSE");
return 0;
}
Thank you Kelly

Looks like you're on Windows OS
.csv file extension is not an executable one. It won't get "executed", even if its present in current working directory/folder.
Probably what you want is a .exe, .com or .bat file.
Here, in this case I think you want your .CPP 's executable with command line argument.
May be something like
C:\Users\MS>c:\test\sp.exe c:\test\sp.csv' Considering your C++ file name is sp.cpp
2 . Looks like you want to display out all contents of sp.scv
You may want to read the header first (i.e the Titles), and then read the values.
There are lot of question already asked on StackOverflow, related to this, please refer them.
Also for proper formatting you may want to use std::setw

Related

Unable to write to file output.txt

I'm trying to write a program. The first file will be opened for input and the second file will be opened for output. (It will be assumed that the first file contains sentences that end with a period.) The program will read the contents of the first file and change all the letters to lowercase except the first letter of each sentence, which should be made uppercase. The revised contents should be stored in the second file.
I've been able to get my code to work in that I successfully converted the contents in input.txt to the above requirements (all sentences are lowercase except for the first word in each sentence). However, this content does not appear in output.txt
Both input.txt and output.txt are in the same directory next to main.cpp.
I tried using different IDEs but that didn't do anything. I also tried moving around the location of output.txt but that did nothing also.
SAMPLE INPUT: google's homepage includes a button labeled "I'm Feeling
Lucky". When a user types in a search AND clicks on the button the
user will be taken directly to the first search result, bypassing the
search engine results page.
SAMPLE OUTPUT: Google's homepage includes a button labeled "i'm
feeling lucky". When a user types in a search and clicks on the
button the user will be taken directly to the first search result,
bypassing the search engine results page.
string inFileName, outFileName;
string line;
char c;
cout << "Enter input file name: ";
cin >> inFileName;
fstream fin, fout;
fin.open(inFileName.c_str(), ios::in);
fout.open(outFileName.c_str(), ios::out);
if (fin.fail())
{
cout << "INPUT FILE DOES NOT EXIST (DNE)\n";
system("pause");
return 1;
}
Nothing shows up in output.txt (it's blank). From what I've noticed this command fout << line << "." << endl; isn't doing anything.
[Here's another screenshot]that shows what's in my terminal as well as what is in input.txt and output.txt:
You'll notice that in the terminal the proper conversion is shown but I am unable to get that text in the terminal into output.txt.
After every source code is compiled, a executable program is created. On Windows, it has .exe extension. The program should have the same name from your source code.
Try to find where it is created. It can be inside the temp folder or maybe where Visual Studio is installed.
Your text document (.txt) file for storing output has to be in the same directory with the executable.
I executed your program on my device (on Code::Blocks IDE though). It went as it should.
I agree with all the above answers and they are appropriate for all the users. In my case though there was a segmentation fault that led to file write failure. Try rectifying any segmentation faults.

How can i solve an issue in writing data in specific file in my clr project?

I am trying to add some data to a specific file in my project. I am doing that in the function below.
void Files::write_employee(employee employeeObject)
{
fstream infile;
infile.open("employeeFile.txt",ios::in|ios::out|ios::app);
string record;
char delimiter='#';
record=employeeObject.get_id()+delimiter;
record+=employeeObject.get_name()+delimiter;
record+=employeeObject.get_password()+delimiter;
record+=employeeObject.get_age()+delimiter;
record+=employeeObject.get_gender()+delimiter;
record+=employeeObject.get_MaritalStatus()+delimiter;
record+=employeeObject.get_ministryName()+delimiter;
record+=employeeObject.get_departmentName()+delimiter;
record+=employeeObject.get_salary()+delimiter;
record+=employeeObject.get_photoPath()+delimiter;
record+=employeeObject.get_photoFileName()+delimiter;
if (infile.fail())exit(1);
else {infile<<record;
infile.close();}
}
This function explains how to add data to my file through save the entered data
in an object and save this values in string record and push it to the file.
The big problem is my file which I am trying to add data in, not created yet.
and I don't know why.
thanks in advance.
You use infile whereas you are outputting to file. While this does not affect the program code, it makes no sense and break your program readability. Use outfile instead.
Remember that it is just like cout << and cin >> for the standard I/O.
Also, try not to use ios::in when your purpose is only to output to the file and vise versa.
According to std::fstream::open example at cplusplus.com, your code is correct and the file must be created. First try to specify an absolute file path to a location that you have write access. If it does not work, print the error message using the following line of code:
cerr << "Error: " << strerror(errno);

How to have a text file created based on user input C++

I'm fairly new to C++. I'm creating a code that will input a file and output the results in an output file, and using stacks and junk.
But what i want to do is create a file based on a user input. Asking the user (when a file doesn't exist in a specific directory) if they would like to create that empty file. I've done this on C# using Directory and Dictionary, but C++ isn't really clicking for me. Here's the snippet of my code (I'm not going to paste 200+ lines for one thing) and where i want to do. Ignore the comments. It's just to keep track of what I'm doing.
if (file.is_open()) //if the file is open (and works)
{
string output;
cout << "Please enter the full directory of the file you would like to have the results in" << endl;
cin >> output;
output.c_str();
file_result.open(output); //open the results file for checking answers
while (file_result.fail())
{
cout << "This file does not exist. Would you like to make one?" << endl;
}
As you see, where I ask the user if they would like to make that file is where i would want this to be.
Any help would be lovely! Transitioning from C# to C++ was a bad idea.
You can open a file for writting (in append mode) in this way:
std::ofstream ofs;
ofs.open (output.c_str(), std::ofstream::out | std::ofstream::app);
ofs << " more lorem ipsum";
ofs.close();
More information about file operations can be found here:
http://www.cplusplus.com/reference/fstream/ofstream/open/
The most basic way to create a file based off of user input is this, You should how ever include checks to make sure the path is valid and that no file exists etc.. I only have time to show you how to do this.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ofstream outFile;
string path;
cout << "Please enter the full path for your file: ";
getline(cin, path);
outFile.open(path);
return 0;
}
What's happening here is quite simple the user inputs the full path (C:\Hello.txt) it's read by getline(cin, path) and is stored in path.
outfile then creates that file.
Please make sure you add checks to validate no file with that name already exists etc.. I'll update this later with a better example but this will create a file for you

How to read many sets of data from a file

I have to read the four data elements viz student name, father's name, roll no and age from a file.
I am using inps as input file stream and outs as output data stream. I have 10 data sets in my input file. But this program writing only 1st data set in output file and ignoring rest 9 sets. Please give some suggestions to this problem.
string line;
int data;
while (inps) {
getline(inps,line); //read from file and put in line
s1.setName(line);
getline(inps,line);
s1.setFatherName(line);
inps>>data;
s1.setRollNo(data);
inps>>data;
s1.setAge(data);
outs.open("output",ios::app);
outs<<"Student name: "<<s1.getName()<<endl<<"Father’s name: "
<<s1.getFatherName()<<endl;
outs<<"Roll number: "<<s1.getRollNo()<<endl<<"Age: "
<<s1.getAge()<<endl<<endl;
outs<<"============================================================="
<<endl<<endl;
}
inps.close();
//write in output file
outs.close();
Don't continually re-open the same ofstream:
at all, it's just silly
certainly not without closing it properly first
Another problem in this program is that while entering in the loop, it checks inps==0 or not and still its uninitialized!
So, instead of while(inps), we have to write for(getline(inps,line);line!="";getline(inps,line))
and then remove very first line of code and it all works nicely.

std::ifstream not reading in after switching text editors from notepad++ to sublime text 2 for using the file it's reading in?

I read some data for my application from file, and it recently stopped working. I feel like the time when it stopped working corresponds to when I switched from Notepad++ to Sublime Text 2... Anyway, here is my code to read in the data:
std::ifstream stream;
stream.open("parsing_model.txt");
char ignore_char;
std::string model_class;
int parsing_model;
while (stream >> model_class >> ignore_char >> parsing_model)
{
// snip
// doesn't even make it into a single run of this while loop.
}
My data is organized as
Item1, 12
Item2, 4
foo, 42
bar, 1
Is it something in the text encoding? How can I make my code robust against this and solve my problem? This code absolutely worked for months up until recently. Thanks
Check to see if the stream is in a good state before using it.
stream.open("parsing_model.txt");
if (stream.good()) {
//... read the stream
} else {
std::cerr << "failed to open input file\n";
}
If there is a failure, make sure the current working directory is the same location as where you have saved the input file. It seems you are running on windows, so you should be able to use this command to view your current directory.
system("dir & pause");