Hello I am following a tutorial on youtube that is beginning with in files and out files.
This is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream infile;
infile.open("numbers.rtf");
//Check for Error
if (infile.fail())
{
cerr << "Error opening file" << endl;
exit(1);
}
int x, y;
infile >> x >> y;
cout << "num 1 =" << x << endl;
cout << "num 2 =" << y << endl;
return 0;
}
I am running on a mac so the .txt file is .rtf. Even after changing that extension the error message still comes up. Heading to school now, appreciate responses
Your code is correct. Has nothing to do with the type of file you are opening.
In case you are compiling from command line, place the file number.rtf in the same folder from which you're launching your executable, and it should find the file.
Otherwise, use a full pathname for the .rtf file.
PS: You can use a .txt file extension on a mac as well. .rtf is just the default.
Related
I'm confused about how to open/access a file using Notepad++ and the cmd with MinGW compiler. I understand the file needs to be in the same scope however, I'm not sure where. I have tried placing the .txt file in my Documents folder which also holds the main.cpp file, and I have tried placing it in the bin folder of the MinGw folder. When I run the code, I get the error message. Is something wrong with my code or is the .txt file in the wrong location?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFS;
ofstream outFS;
string fileName;
double fileNum;
//fileName = "input_prac.txt";
//cout << "Enter file name: " << endl;
//cin >> fileName;
cout << "Opening file..." << endl;
inFS.open("input_prac.txt"); // Open file
if (!inFS.is_open())
{
cout << "Could not open file" << endl;
exit(1);
}
// Read file
while(!inFS.eof())
{
inFS >> fileNum;
cout << fileNum << endl;
}
inFS.close(); // close file
return 0;
}
I'm just learning and practicing the basics of ifstream objects and tried to write a small program that will read a .txt file and then display what is read. I can't tell what I need to change, but i do know that there might be something wrong with how I'm specifying the path because. When I run the program, it doesn't seem like it can open the file. the friends.txt file I made is in the path I specified. In the .txt file, there are 3 names which are written on 3 consecutive lines (after typing one name, I pressed enter to write another name on a new line).
I tried checking to see if made any typos on the folder names in the path and made sure to correct any I found.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputfile;
string name;
inputfile.open("C:\\Users\\Dox\\Documents\\Visual Studio 2015\\Projects\\Practice\\Practice\\friends.txt");
cout << "Reading data from the file.\n";
if (!inputfile.is_open())
{
cout << "error opening file" << endl;
}
else
{
inputfile >> name;
cout << name << endl;
inputfile >> name;
cout << name << endl;
inputfile >> name;
cout << name << endl;
}
inputfile.close();
cin.get();
return 0;
}
When running the code, the error message I made displays.
I am writing a program that needs to read in a list of 11,000 numbers from a text file and then output them to the console. However, whenever I run my code, I have been able to pinpoint that it only prints the last 299 numbers. Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double dataVector[11000]; //text file has 11,000 elements
string userfile; //name of file selected by user
//prompt user for file to be opened
cout << "Enter the name of the file you would like to read :: ";
cin >> userfile;
ifstream ifs(userfile); //open file
if (!ifs) // return error message if file cannot be opened
{
cerr << "Error: could not find the specified file" << endl;
return 1;
}
for (int i = 0; i < 11000; i++)
{
if (ifs >> dataVector[i]) //read in array
cout << dataVector[i] << endl;
else //if element cannot be read, return error
{
cout << "Failed to read." << endl;
break;
}
}
ifs.close(); //closes the file
system("pause");
return 0;
}
Is there something that I'm missing that's causing this issue? My code is not returning any compiler errors, no errors from my checks, and my text file IS in the right location.
I'm a beginner programmer and I'm having an issue that I'm sure is just caused by a stupid mistake but I for the life of me can't figure out what it is. I've tried searching solutions on this site but none of the remedies I found worked for my problem
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
float num;
inputFile.open("numbers.txt");
inputFile >> num;
cout << num << endl;
inputFile >> num;
cout << num << endl;
inputFile >> num;
cout << num << endl;
inputFile >> num;
cout << num << endl;
inputFile >> num;
cout << num << endl;
inputFile.close();
return 0;
}
So it's a simple code, and I'm reading the textbook I have verbatim on what to do but whenever I run the code it just churns out garbage versus the numbers.txt file I created using a different program.
If anyone can tell me where my mistake is, or tell me where to look up this problem I'd appreciate it. Thanks in advance
Edit: I used a different code to create the file and I can confirm that the file was created successfully in .txt format
Please check if the file is opened correctly. Add the below code after inputFile.open("numbers.txt");
if(inputFile.fail())
{
cerr<< "Error Opening File" << endl;
exit(1);
}
Also try giving the absolute path of the file if there is an error in opening the file.
I need to do this: Open a user-specified file for input. Prompt for the name of the file, read it into a string variable, echo print it to the terminal and then open it. If the file is not successfully opened, enter into a loop that prints out an error message, resets the input file stream variable (Input_file_stream_var.clear(); Where Input_file_stream_var is the name of your input file stream variable), obtains a new file name and tries to open the new file. The loop continues until the user successfully enters a valid file name or presses ctrl-c to exit.
and here is the code that i have so far but i cant get it to loop back into the process if the file was not opened.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
// Variables
char test;
string infname, outfname;
fstream infile, outfile;
do
{
// Propt for and echo print input file
cout << endl << "Enter the name of the input file: ";
cin >> infname;
cout << infname << endl;
infile.open(infname.c_str());
// Test if file opened
if(!infile)
{
cout << string(12,'*') << " File Open Error " << string(12,'*') << endl;
cout << "==> Input file failed to open properly!!\n";
cout << "==> Attempted to open file: " << infname << endl;
cout << "==> Please try again...\n";
cout << string(41,'*') << endl;
infile.clear();
return 1;
}
} while(!infile.is_open());
return 0;
}
The return 1 statement is causing your program to exit: you are returning from main()
Just don't do that.
Try removing the return 1 or changing it to continue. return 1 returns code execution from main and not the loop.