C++: Text file not being read - c++

I'm trying to print string values from a text file to the console but the file isn't opening.
#include "stdafx.h"
#include <iostream>
#include <string>
#include<fstream>
int main()
{
ifstream nameFile;
nameFile.open("E:\\Names.txt");
string a;
int studentCount = 0;
if (nameFile.is_open())
{
while (getline(nameFile, a)) {
cout << a << '\n';
studentCount++;
}
cout << studentCount;
}
else
cerr << "Unable to load file" << endl;}

Related

"'main': identifier not found" error in c++

I keep getting this error and have no idea how to fix it because I don't see anything wrong with my code.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
void file_create(std::string name) {
std::ifstream file(name);
if (file.is_open()) {
file.close();
std::cout << "File already exists..." << std::endl;
main();
}
else {
file.close(); std::ofstream newFile(name);
if (newFile.is_open())
std::cout << GREEN "New file successfully created..." << RESET << std::endl;
else
std::cout << RED "File could not be created" << RESET << std::endl;
newFile.close();
}
}
int main() {
}
The main function is not intended to be invoked from your code. It is also a nonsense since it is called automatically when program starts. But if you need some alternative main to be invoked upon some condition, you can call it inside your main function
...
int alternative_main()
{
place your program there
}
...
void file_create(std::string name) {
...
if (file.is_open()) {
...
alternative_main()
}
else {
...
}
}
Suppose your main looks like this
int main() {
file_create("myfile");
}
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
int main(); // defined before file_create
void file_create(std::string name) {
std::ifstream file(name);
if (file.is_open()) {
file.close();
std::cout << "File already exists..." << std::endl;
main();
}
else {
file.close(); std::ofstream newFile(name);
if (newFile.is_open())
std::cout << GREEN "New file successfully created..." << RESET << std::endl;
else
std::cout << RED "File could not be created" << RESET << std::endl;
newFile.close();
}
}
int main() {
}
Define int main() above file_create() so you can call it in file_create
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
//use this instead of repetitively using std all the time
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
void file_create(string name) {
ifstream file(name);
if (file.is_open()) {
file.close();
cout << "File already exists..." << endl;
return 0;
}
else {
file.close();
ofstream newFile(name);
if (newFile.is_open())
cout << GREEN "New file successfully created..." << RESET << endl;
else
cout << RED "File could not be created" << RESET << endl;
newFile.close();
}
}
int main() {
string name;
cin>>name;
file_create(name);
}
try using this way...

Creating new txt files with c++ program, naming with variables

I have created a program and I want to create with it files like aff1.txt, aff2.txt, etc. In these files, I want to have here a text created this way: It will open the file: text.txt and it will take each sentence, copy it 4700/sentence length times to each file. But it isn't working, when: cout << ss << endl;, it writes to cmd nothing, while there should be something, which was assigned before. What should I do?
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;
int main()
{
ifstream vstup("text.txt"); // 4700,2700,2200,1700
string vety;
getline(vstup,vety);
vstup.close();
string ss="affn.txt";
char q[vety.length()];
for (int u=0;u<vety.length();u++)
{
q[u] = vety[u];
}
int l=0,m=0,n=0;
int v,i,e,o;
char vl[999999];
//cout << vety.length() << endl;
for (i=0;i<vety.length();i++)
{
//cout << "ss" << endl;
if (q[i]=='.')
{
// cout << "ss" << endl;
v=4700/i;
for (e=0;e<v;e++)
{
//cout << "ss" << endl;
for (o=0;o<i-l;o++)
{
// cout << "ss" << endl;
m=o+e*(i-l);
vl[m]=q[o+l];
}
}
l=l+i;
cout << vl << endl;
n++;
//ofstream aff("aff.txt");
//aff << vl << endl;
//aff.close();
ss[3]=n;
ofstream writer(ss.c_str());
//writer.open(ss.c_str());
writer << vl << endl;
writer.close();
cout << ss << endl;
ss.clear();
}
}
return 0;
}

Reading lines from file in C++

Why can't I get each line in a file printed with this? It outputs nothing.
string line;
ifstream s {"book_list.txt"};
while (getline(s, line)) {
cout << line << endl;
}
I've included fstream, sstream, string, stdio.h, stdlib.h and am using namespace std;
#include<iostream>
#include<fstream>
#include<string>
using namespace std
int main()
{
string line;
ifstream s ("book_list.txt");
if (s.is_open())
{
while ( getline (s,line) )
{
cout << line << endl;
}
s.close(); // Don't forget to close the file
}
else
cout << "Unable to open file";
return 0;
}
I hop it helps.
I guess the file doesnt exist.
Please check with:
if(!s.good()) {
// error
}
or
while (s.good()) {
// process.
}
You should do something like this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string sLine = "";
ifstream infile;
infile.open("temp.txt");
if(infile.is_open()){
while (getline(infile, sLine))
{
cout << sLine << endl;
}
}
infile.close();
cout << "Read file completed!!" << endl;
}
#include <iostream>
#include <fsstream>
using namespace std;
int main()
{
string data;
int counter = 0;
ifstream infile("test_file.txt");
while (!infile.eof()){
getline(infile , data);
counter += 1;
}
cout << "The number of lines is : " << counter << endl;
return 0;
}

ifstream ofstream on mac

I'm trying to read in a random file (on mac-xcode) and determine the instances of the letter k in the document. Then print the number as an outout file. My problem is that the outfile isn't being written and the nums_k is coming back as 0. I'm not sure if the ifstream is working incorrectly or the ofstream need a different filename established. Here's my source code.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile("Users/bryanmichaelnorris/documents/extra credit assignment.docx");
string line;
int numks = 0;
while (getline(infile,line)) {
int x = 0;
for (std::string::iterator it=line.begin(); it!=line.end(); ++it) {
if (line[x] == 'k') {
numks++;
}
x++;
}
}
infile.close();
ofstream outfile("number of k's.docx");
outfile << "There are " << numks << " K's in the file." << endl;
outfile.close();
return 0;
}
Added validations for the opened files.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const char * csInputFileNane="Users/bryanmichaelnorris/documents/extra credit assignment.docx";
ifstream infile(csInputFileNane);
if (!infile.is_open()) {
cerr << "Cannot open file \""<<csInputFileNane<<'"'<<endl;
return -1;
}
string line;
int numks = 0;
while (getline(infile,line))
{ int x = 0;
for (std::string::iterator it=line.begin(); it!=line.end(); ++it) {
if (line[x] == 'k')
{
numks++;
}
x++;
}
}
infile.close();
const char *csOutFileName="number of k's.docx";
ofstream outfile(csOutFileName);
if (!outfile.is_open()) {
cerr << "Cannot open file \""<<csOutFileName<<'"'<<endl;
return -1;
}
outfile << "There are " << numks << " K's in the file." << endl;
outfile.close();
return 0;
}

C++ Write sorted contents of vector<string> to file

This currently reads a .txt file and sorts the contents. I'm trying to get it to write those sorted contents of the vector to a file. Currently it only writes one line, how can I can get it to put all lines in the new file? Thank you so much. -Kaiya
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
using namespace std;
inline void keep_window_open() {char ch; cin>>ch;}
int main()
{
string line;
ifstream myfile("weblog.txt");
vector<string> fileLines;
//stack overflow example
if (!myfile) //test the file
{
cout << "Unable to open the file" << endl;
return 0;
}
while (getline(myfile, line))
{
fileLines.push_back(line);
//cout << line << '\n';
}
sort(fileLines.begin(), fileLines.end()); //sorting string vector
for (string &s : fileLines)
{
cout << s << " ";
ofstream newfile ("newfile.txt");
newfile << s << " ";
};
return 0;
}
ofstream newfile ("newfile.txt");
for (string &s : fileLines)
{
cout << s << " ";
newfile << s << " ";
};
Creating newfile for every loop iteration overwrites the content of the file, by default.
Either open newfile before the last loop, or open it in append mode within the loop.
It's because you are creating a new file in each iteration of your loop!
ofstream newfile("newfile.txt");
should be written before the loop.
ofstream newfile ("newfile.txt");
for (string &s : fileLines)
{
cout << s << " ";
newfile << s << " ";
};
ofstream newfile ("newfile.txt");
copy(fileLines.begin(), fileLines.end(), ostream_iterator<string>(newfile, " ") );
Here is my complete code that worked, thanks Xiaotian Pei for your help.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
using namespace std;
inline void keep_window_open() {char ch; cin>>ch;}
int main()
{
string line;
ifstream myfile("weblog.txt");
vector<string> fileLines;
if (!myfile) //test the file
{
cout << "Unable to open the file" << endl;
return 0;
}
while (getline(myfile, line))
{
fileLines.push_back(line);
}
sort(fileLines.begin(), fileLines.end()); //sorting string vector
ofstream newfile ("newfile.txt"); //write to new file
for (string &s : fileLines)
{
cout << s << " ";
newfile << s << " ";
}
return 0;
}