Getline only printing out last line when called multiple times - c++

So this is a fairly simple example of a program where I'm trying to output the first two lines of an input text file. The ifstream should be a global variable, and the testGetFile() function is necessary (I have not done the actual text processing needed in this code.) I'm trying to figure out why this is cout-ing only the SECOND line of the input file. Any help will be appreciated!
Thanks in advance!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
ifstream input;
string testGetFile(){
string result;
getline(input,result);
return result;
}
int main(){
input.open("testInput.txt");
cout<< testGetFile();
cout<< testGetFile();
return 0;
}

Related

C++ - work with file (read file) then show in the console

I would like to read the content from the text file by C++ , I wrote the code as below:
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
ifstream input("C:\\Users\\thang\\Desktop\\Thang\\output.txt");
string str;
while(!input.eof()) //check if it goes to the end of the file
{
//getline(input,str); //
input>>str; // get the value of str from the file
cout<<str<<endl;
}
return 0;
}
But it just show me the result with no empty row , but the output.txt file has the empty line.
I have attached the screenshot
Could you please help explain why it get the result without the empty line ? Appriciated for all assist.
Because the operator>> to read std::string from std::ifstream skips leading whitespaces. Newline characters is one kind of whitespace characters, so they are ignored.
Because input>>str strips away any leading whitespace.
If you just need to read the file like cat does, use rdbuf directly:
ifstream input(...);
std::cout<<input.rdbuf()<<std::endl;
If you want to iterate over the lines, including the empty ones, use std::getline
ifstream input(...);
for (std::string line; std::getline(input, line); )
{
...
}

Getting Information from input file C++

I'm pretty new to coding so I'm not entirely sure if I'm doing file extraction correct. I'm getting lldb as my output for this code. Instead of prompting the user with the words in the hangman.dat file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream sourceFile;
sourceFile.open("hangman.dat");
if (sourceFile.fail())
{
cout<<"File didn't open" ;
}
else
{
string words;
sourceFile >> words;
while(sourceFile>>words)
{
cout<<words<<endl;
}
}
}
The file hangman.dat contains the following information:
Fall
leaves
Thanksgiving
pumpkins
turkey
Halloween

Not Fully read from the text file

I am trying to read some integer numbers from a text file in C++. It is weird problem that it reads only 13 numbers , but my file contains 25 numbers . I searched but not found any thing , someone suggested to add ios::binary , but not working.
Why ??
here is this part of code.
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
ifstream myfile;
myfile.open("Nvector.txt");
vector<int> N;
for(int j=0; j<25; j++)
{
int input;
myfile>> input;
N.push_back (input);
}
system("PAUSE");
return 0;
}
You might want to check the text file that you're reading from, there can be special end of line characters that will cause the integers to be concatenated/removed from the vector, there might also be problems related to the vector

File Handling C++ Error

I wanted write a program which allows users to write some random stuff, but i got an
error saying no matching call to which I am not able to figure it out. please Help me.
while you are trying to answer to this question, try to be more noob specific.
here is my code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string story;
ofstream theFile;
theFile.open("Random.txt");
while(cin.get(story,5000)!=EOF)
{
theFile<< story;
}
return 0;
}
cin.get with 2 arguments expects char* as first argument and you are trying to pass string as the first argument.
If you want to read std::string instead of C-string until the end of line use getline(cin, story)
If you want to read string until the next space or newline or another blank symbol use cin >> story;
You seem to be trying to write the content of cin to a file. You could just use stream operators:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string story;
ofstream theFile;
theFile.open("Random.txt");
if(cin >> story)
{
theFile << story.substr(0, 5000);
}
return 0;
}
I am assuming that you only want the first 5000 characters in Random.txt...

C++ Program help: Structs, arrays, loops, input files, loop not working?

I am trying to write a program which will declare an array of 5 structs from information read from a file. Then I use a loop to the print the information of every element in the array.
The code I have written only seems to read one line from the txt. file. Any tips or advice would be appreciated.
#include <istream>
#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
struct Bankinfo{
string name;
int accountnum;
float checking;
float savings;
string phone;
} bankinfo[5];
int i;
i=0;
cout<<"This is a test program"<<endl;
char x;
x=0;
for (i=0;i<=6;i++)
{
ifstream infile;
char testinfo [10001];
infile.open("testinfo.txt");
cin.get(testinfo,10001);
cout<<testinfo<<endl;
infile>>bankinfo [i].name>>bankinfo [i].accountnum>>bankinfo [i].checking>>bankinfo [i].savings>>bankinfo [i].phone;
cout<<setw(10) << (bankinfo[i].name);
cout<<setw(10) <<(bankinfo [i].accountnum);
cout<<setw(10) <<(bankinfo [i].checking);
cout<<setw(10) <<setprecision (2)<<fixed<<(bankinfo [i].savings);
cout<<setw(15) <<(bankinfo [i].phone);
}
cout<<" "<<endl;
cout<<"Thanks for using the program"<<endl;
return (0);
}
You're opening the file in each iteration of the loop in i. Try to get out of the loop the infile.open(...). Now it will read more lines. I don't see the purpose of that cin.get(...) either.