I' m training with file in C++. I have a problem: If I write more than 1 word in the terminal, in the file I have just the first written word.
Example
Hello devs
in the file I will have only
Hello
and the next time, that I'll execute the programme the new word will be in the next row.
Code
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
/*
*
*/
main(int argc, char** argv) {
string test;
string st;
ofstream biblio;
biblio.open("lis.txt", ios::app);
if(biblio.is_open()){
cout <<" Write in file: ";
cin >> test;
biblio << test << "\n";
biblio.close();
cout<<"I'm in the if!!!\n";
}
ifstream biblio1;
biblio1.open("list.txt");
if(biblio1.is_open()){
while(getline(biblio1,st)){
cout << st << '\n';
}
cout<<"I'm in the other if!!!\n";
biblio1.close();
}
else{
cout<<"Not possible to open the file\n";
}
}
operator >> uses white space to delineate items, use std::getline(std::cin, test) instead.
Related
I am trying to write a program that will read a .txt file that is called when the program is run from terminal.
the command used will be;
$ ./myexecutable input.txt
My program and the input.txt are in the same directory. My code so far is as follows
#include <string>
#include <fstream>
using namespace std;
int main(int argc , char* argv[]){
string temp = "here";
string filename = argv[1];
ifstream myFile (filename);
myFile.open(filename);
if (myFile.is_open()){
while (getline (myFile, temp)){
cout << temp << endl;
myFile.close();
}
} else {
cout <<< "You have Entered Wrong File Name" << endl;
}
cout << "do with the simple program" << endl;
return 1;
};
but the output I get is just
file opened, do with the simple program
I am not really familiar with fstream so don't know where I may have gone wrong. I followed their tutorial found here.
but clearly I've done something wrong.
Thanks for your help.
This is the corrected code!!
You were opening myFile twice!!
First time in this statement ifstream myFile (filename);
Second time in this statement-myFile.open(filename);
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc , char* argv[]){
string temp = "here";
string filename = argv[1];
ifstream myFile (filename);
if (myFile.is_open()){
while (getline (myFile, temp)){
cout << temp << endl;
myFile.close();
}
} else {
cout << "You have Entered Wrong File Name" << endl;
}
cout << "do with the simple program" << endl;
return 1;
};
I have an issue with some code I have been working on. I am trying to read the contents of a text file (input.txt) into a variable fileContents. The loop in the code enters, but the program produces no output. Some of the variables are not used, I know about this. What is wrong?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
ifstream input("input.txt");
ofstream output("output.txt"); //init output controller
// new lines will be skipped unless we stop it from happening:
//input.unsetf(std::ios_base::skipws);
// count the newlines with an algorithm specialized for counting:
unsigned line_count = std::count(std::istream_iterator<char>(input),std::istream_iterator<char>(), '\n');
string fileContents = ""; //init message, that will be filled by input.txt
string str; //temp string
while (input >> fileContents)
{
//cout << "loop entered";
cout << fileContents << "\n";
}
//cout << "test" << "\n";
return 0;
}
I have this code that counts the number of pattern occurrence in textfile.
#include <iostream>
int main()
{
// std::cout << "Hello World!" << std::endl;
// return 0;
ifstream fin("my_data.txt"); //opening text file
int count=0;
char ch[20],c[20];
cout<<"Enter a word to count:";
gets(c);
while(fin)
{
fin>>ch;
if(strcmp(ch,c)==0)
count++;
}
cout<<"Occurrence="<<count<<"n";
fin.close(); //closing file
return 0;
}
However, upon testing I got this error
10 2 C:\Users\80977432\Documents\C++\Untitled1.cpp [Error] 'ifstream' was not declared in this scope
ifstream cout strcmp , etc all belongs to namespace std.
So use std::ifstream, std::cout , etc
Also use #include <fstream> for file I/O operation
#include<cstring> for std::strcmp
I wrote a quick C++ program that asks the user for a input text file and an output text file. The program is then supposed to number the lines in the file on the left margin. However, I cannot seem to get it working properly, it compiles fine but does not number the lines like it is supposed to. I believe it is a logical error on my part. I am also not too familiar with file i/o in C++ as I am just learning it now using old school textbooks.
Here is the file:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>
using namespace std;
int main(void)
{int i = 0 , num = 1;
string inputFileName;
string outputFileName;
string s;
ifstream fileIn;
ofstream fileOut;
char ch;
cout<<"Enter name of input file: ";
cin>>inputFileName;
cout<<"Enter name of output file: ";
cin>>outputFileName;
fileIn.open(inputFileName.data());
fileOut.open(outputFileName.data());
assert(fileIn.is_open() );
assert(fileOut.is_open() );
while (!(fileIn.eof()))
{ch=fileIn.get();
if (ch=='\n') num++;
fileOut << num << "\n";
s.insert(i,1,ch); //insert character at position i
i++;
}
fileOut << s;
fileIn.close();
fileOut.close();
return 0;
}
If anyone could point me in thr right direction or give me some tips I would be eternally grateful.
int i = 0;
string line;
while (getline(infile, line))
{
outfile << (i++) << " " << line << "\n";
}
So Lets say this is what the input file contains
12
Hello
45
54
100
Cheese
23
How would I print it out on the screen in that order.
This is what I had but it skips some lines.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int number;
string word;
int loop = 0;
ifstream infile;
infile.open("arraynumbers.txt");
while(infile >> number >> word)
{
if( infile >> number)
{
cout << number << endl;
}
if(infile >> word)
{
cout << word << endl;
}
}
return 0;
}
I suggest using www.cplusplus.com to answer these questions.
However, you are on the right track. Since you are just outputting the contents of the file to stdout, I suggest using readline() and a string. If you need to access the numeric strings as ints, use the atoi() function.
Example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
ifstream file("arraynumber.txt");
if (file.is_open()) {
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else cout << "Error opening arraynumber.txt: File not found in current directory\n";
return 0;