I'm using fstream to access a file and extract its contents. When I go to output its data I continue to get just one weird symbol. This is the process I'm using. I've used it before with success but now I seem to have an issue. Here is the code.
#include<iostream>
#include<fstream>
using namespace std;
int main() {
char text;
int waitForIt;
fstream Txt1("In.txt", ios::in);
cout << "\n\tcontents of In.txt:" << endl << endl;
cout << "\t\t";
Txt1.get(text);
do {
cout << text;
Txt1.get(text);
} while (!Txt1.eof());
Txt1.close();
cin >> waitForIt;
};
This is what is being output:
I bet your file could not be opened. The way you wrote your loop, you print the character you think you had read by using get function even if reading failed.
You should do:
fstream Txt1("In.txt", ios::in);
if ( Txt1.is_open() )
{
cout << "\n\tcontents of In.txt:" << endl << endl;
cout << "\t\t";
while (!Txt1.eof())
{
Txt1.get(text);
cout << text;
}
Txt1.close();
}
else
{
cout << "Unable to open file" << endl;
}
Related
My program creates a file and names it after user input, after that it will continue to loop to and ask if you want to keep adding to the file created. after you say no, then it will close the file and read the first three lines of the file created. My program does not output anything into the file, it appears empty and does read three lines but they are the last user inputted line but three times. How to fix this? Here is my code.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
//Global Variables
fstream File;
string fileName, writeToFile;
char choice;
void write();
void read();
void exitprogram();
int main()
{
// Name and open the output file
cout << "What is the file name that you want to create?" << endl;
cin >> fileName;
cout << "File " << fileName << " has been created!" << endl;
ofstream File(fileName.c_str());
File.open(fileName.c_str());
if (File.is_open())
{
cout << "File opened successfully." << endl;
write();
}
else
{
cout << "Unable to open file" << endl;
}
return 0;
}
void write()
{
choice = ' ';
cout << "What do you want to add?: " << endl;
getline(cin.ignore(),writeToFile);
File << writeToFile << endl;
cout << "Do you want to add something else to the file (1=YES or 2=NO): ";
cin >> choice;
switch(choice)
{
case '1' :
write();
break;
case '2' :
cout << "Saving..." << endl;
File.close();
read();
break;
}
}
void read()
{
cout <<"Here are the first there lines of file " << fileName << endl;
for (int counter = 1; counter <= 3; counter++)
{
File >> writeToFile;
cout << writeToFile << endl;
}
exitprogram();
}
void exitprogram()
{
exit(0);
}
You shadowed the global File used by write by declaring another in main. (The moral is not to use global variables—pretty much never. Pass parameters instead, although most of yours can just be made local anyway.)
I attempted to create a program that opens a file called "hours1.txt" which reads all the data from this file storing it into a vector. Then it should output all the data to a file called "payments1.txt". Although, I cant even get the file to open I dont know what im doing wrong.
using namespace std;
struct PayRecords{
char lastName[50];
char firstName[50];
double hW;
double hoW;
};
int main()
{
vector<PayRecords>payInfo; // creating an empty vector that will hold the data..
PayRecords data; // creating an object that can access each member of the structure..
ifstream infile; // declaring an object that can open the file..
infile.open("hours1.txt"); // opening the file..
ofstream outfile;
outfile.open("payments1.txt");
if(!infile)
{
cerr << "Sorry this file could not be opened! Maybe its not placed in the right folder???" << endl;
return 1;
}
while(infile >> data.lastName >> data.firstName >> data.hW >> data.hoW) // while we arent at the end of the file, keep reading each piece of new data..
{
payInfo.push_back(data); // adding the data to each member of the structure into the vector..
}
for(int i=0; i<payInfo.size(); i++) // this for loop is displaying all the results
{
outfile << "YOUR NAME" << endl;
outfile << "PAY FOR THIS WEEK" << endl;
outfile << "==========================" << endl << endl;
outfile << payInfo[i].lastName << endl;
outfile << payInfo[i].firstName << endl;
outfile << payInfo[i].hW << endl;
outfile << payInfo[i].hoW << endl;
}
cout << payInfo.size();
infile.close();
outfile.close();
cout << "thank you the file has been changed";
}
How am I able to fix the loop to ask the user to retype the filename over and over again if they type in the wrong file name?
using namespace std;
void get_input_file(ifstream &in_stream);
int main()
{
ifstream in_stream;
cout << "Welcome to the Test Grader." << endl;
get_input_file(in_stream);
}
void get_input_file(ifstream &in_stream) {
string file_name;
do {
cout << "Enter the file name you would like to import the data from: " << endl;
cin >> file_name;
in_stream.open(file_name.c_str()); //Opens the input file into the stream}
}
while (in_stream.fail()); {
cout << "Error finding file, try again.\n";
}
cout << "Testing: " << endl;
cout << file_name << endl;
}
maybe this:
using namespace std;
void get_input_file(ifstream &in_stream);
int main()
{
ifstream in_stream;
cout << "Welcome to the Test Grader." << endl;
get_input_file(in_stream);
}
void get_input_file(ifstream &in_stream)
{
string file_name;
do
{
cout << "Enter the file name you would like to import the data from: " << endl;
cin >> file_name;
in_stream.open(file_name.c_str()); //Opens the input file into the stream
if(in_stream.fail())
{
cout << "Error finding file, try again.\n";
continue;
}
break;
} while(true);
cout << "Testing: " << endl;
cout << file_name << endl;
}
I don't think your do while loop does what you'd like it to do.
After the semicolon your loop is over, hence the block behind it is not executed within the loop.
What I think you are looking for is something like this:
do {
cout << "Enter the file name you would like to import the data from: " << endl;
cin >> file_name;
in_stream.open(file_name.c_str()); //Opens the input file into the stream}
if(in_stream.fail()){
cout << "Error finding file, try again.\n";
}
} while (in_stream.fail());
So I am trying to read from a text file. It shows that I can successfully read from the file But when I try to cout the values, it just shows 0, while I have other values in the text file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ifstream file("numbers.txt");
if (file) {
cout << "Managed to read file successfully. \n";
}else{
cout << "Unable to read file.";
}
int x, y;
file >> x >> y;
cout << "Num 1: " << x << endl;
cout << "Num 2: " << y << endl;
return 0;
}
With the numbers.txt file
45
15
Your code work find with gcc.
int main()
{
std::ifstream file("numbers.txt");
if (file)
{
cout << "Managed to read file successfully. \n";
int x, y;
file >> x >> y;
cout << "Num 1: " << x << endl;
cout << "Num 2: " << y << endl;
}
else
{
cout << "Unable to read file.";
}
return 0;
}
The above code will print status of your operation,if you want to read and display contents the you have to write your code like this:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream stream1("D:\\file1.txt");
char a[80];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
while(!stream1.eof())
{
stream1 >> a;
cout << a << endl;
}
return(0);
}
I am trying to use fstream but am running into problems when trying to open a file from within Visual Studio 2013. In Visual Studio, I have two resources that I have enabled to be used in the project titeld input1.txt and input2.txt . If I directly run the application from the Debug folder using File Explorer, I am able to use the ifles. If I try to run it from within Visual Studio with ctrl, neither files can be found. I believe my code is correct, but I'm not sure what changes to make to the project to have it run correctly.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const bool VERBOSE(true);
int main(){
ifstream input;
ofstream output;
string inFileName;
string outFileName;
string tempString;
// Get input file name into a string
cout << "Input file name: " << flush;
cin >> inFileName;
if (VERBOSE)
{
cout << "Input file name is " << inFileName << endl;
}
// Convert filenames to C strings and use stream.open()
input.open(inFileName.c_str());
if (input.fail())
{
cout << "File " << inFileName << " cannot be opened" << endl;
return -1;
}
// Get output file name into a string
cout << "Output file name: " << flush;
cin >> outFileName;
if (VERBOSE)
{
cout << "Output file name is " << outFileName << endl;
}
// Convert filenames to C strings and use stream.open()
// When opening output file, it will create the file if it
// does not exist, and will clobber it if it does.
output.open(outFileName.c_str());
if (output.fail())
{
cout << "File " << outFileName << " cannot be opened" << endl;
return -2;
}
// While there is more to the input file, get a word
// and copy it to the output file on its own line.
while (!input.eof())
{
input >> tempString;
if (VERBOSE)
{
cout << " Length is " << tempString.length() << " for " << flush;
}
if (tempString.length() > 0)
{
if (VERBOSE)
{
cout << tempString << endl;
}
output << tempString << endl;
}
else
{
if (VERBOSE)
{
cout << "No more input" << endl;
}
}
// This is needed to keep the last non-whitespace word
// read in from being printed twice if the file ends in
// whitespace, including a newline.
tempString.clear();
}
cout << endl;
return 0;
}
Specify the full path to the files when you cin them.