How can I open files using filestream in Notepad++ and cmd? C++ - c++

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;
}

Related

Rename and remove function for the txt file is not working c++

I know this is asked a lot, but I am spending hours to solve this problem. I am trying to edit txt file by replacing names. I copied my datas to the temp.txt file and when I enter the inputs, temp file does the job and changing the word. But the functions which are remove and rename are not working. My code is below:
string search_string;
string replace_string;
ofstream file;
file.open("temp.txt"); //opening file
cout<<"Enter the word you want to change: ";
cin>>search_string;
cout<<"Enter the new word: ";
cin>>replace_string;
string inbuf;
fstream input_file("musics.txt", ios::in);
ofstream output_file("temp.txt");
while (!input_file.eof())
{
getline(input_file, inbuf);
int spot = inbuf.find(search_string);
if(spot >= 0)
{
'this is the replacing part'
}
output_file << inbuf << endl;
remove("musics.txt");
file.close();
rename("temp.txt", "musics.txt");
}
You have 2 main problems.
You open the output file twice.
You must close all files before you do file operations
Especially the 2nd topic is causing the trouble.
Here a working solution:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <regex>
// The filenames
const std::string musicFileName{ "music.txt" };
const std::string tempFileName{ "temp.txt" };
int main() {
// Open a new scope, so that the file streams
// will be closed automatically by the destructor
{
// Open the source file
std::ifstream sourceFile(musicFileName);
// Check, if it could be opened
if (sourceFile) {
// Source file is open, now open the temp file
std::ofstream tempFile(tempFileName);
// Check, if the tempfile could be opened
if (tempFile) {
// Both files are open, get the search and replace strings
std::string searchString;
std::string replaceString;
std::cout << "Enter the word you want to change: ";
std::cin >> searchString;
std::cout << "Enter the new word: ";
std::cin >> replaceString;
// Now read all lines from source file
std::string textLine{};
while (std::getline(sourceFile, textLine)) {
// Replace the text and write it to the destination file
tempFile << std::regex_replace(textLine, std::regex(searchString), replaceString) << "\n";
}
}
else {
std::cerr << "Could not open '" << tempFileName << "'\n";
}
} // <-- This will close the temp file
else {
std::cerr << "Could not open '" << musicFileName << "'\n";
}
} // <-- This will close the source file
// Remove and rename
std::remove(musicFileName.c_str());
std::rename(tempFileName.c_str(), musicFileName.c_str());
return 0;
}

understanding ifstream objects and reading from files

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.

Why is my file not being read from the beginning?

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.

Why is this loop not looping?

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.

infile won't open in c++ on mac

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.