I wanted to know how you skip lines as you open the file with fstream. when I open the file it returns all the numbers bunched up together like "201051535402530"
here is my code.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string testing;
ifstream myfile;
myfile.open ("inputfile");
while (!myfile.eof())
{
getline(myfile, testing);
cout << testing;
}
return 0;
}
the inputfile is listed as:
20
10
5
15
35
40
25
30
When getline() reads a line, it discards the trailing newline. If you want one printed in your output, you need to put it there yourself:
cout << testing << endl;
Related
okay I've searched everywhere and couldn't get my hand on it so ..
i'm doing a library system where a librarian enters his username and the program checks if he is one of the librarians or not
i'm stuck on the comparing part , i tried using getline but it gave me an error , tried gets_s and used a char array instead of a string and still didn't work
kindly help me with what i should do
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
int main()
{
//opening files
ifstream readUsername;
ofstream enterUsername;
//variables
string existUsername;
string enteredUsername;
//reading files
readUsername.open("librarian usernames.txt");
if (readUsername.fail())
{
cout << "can't open file" << endl;
}
enterUsername.open("entered librarian username.txt");
if (enterUsername.fail())
{
cout << "can't open file" << endl;
}
while(!readUsername.eof)
{
readUsername >> existUsername;
}
enterUsername << enteredUsername;
readUsername.close();
enterUsername.close();
enterUsername.clear();
system("pause");
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
infile.open("listOfWords.txt"); //open file
for(string listOfWords; getline(infile, listOfWords, '.'); ) //read sentences including
//spaces
cout<<listOfWords; //this displays
return 0;
}
This shows you how to output the text so you should just save both files to a variable then compare the variables.
How can I read a txt file that contains non-English strings? After getting the string I will store it in a linked list, so it should be suitable for storing in a node either, then print it.
When I try the get string "türkçe" from the .txt file code below, it gives the output of:
output: tⁿrkτe
**word.txt**
türkçe
<string>
<iostream>
<fstream>
int main() {
fstream inputFile;
inputFile.open(word.txt);
string line;
getline(inputFile,line);
cout << line << endl;
return 0;
}
The solution of the problem:
#include <string>
#include <iostream>
#include <fstream>
#include <locale.h>
using namespace std;
int main() {
setlocale(LC_ALL, "turkish");
fstream inputFile;
inputFile.open("word.txt");
string line;
getline(inputFile,line);
cout << line << endl;
return 0;
}
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;
I am trying to read all integers from a file and put them into an array. I have an input file that contains integers in the following format:
3 74
74 1
1 74
8 76
Basically, each line contains a number, a space, then another number.
I know in Java I can use the Scanner method nextInt() to ignore the spacing, but I have found no such function in C++.
#include <fstream>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> arr;
std::ifstream f("file.txt");
int i;
while (f >> i)
arr.push_back(i);
}
Or, using standard algorithms:
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
int main()
{
std::vector<int> arr;
std::ifstream f("file.txt");
std::copy(
std::istream_iterator<int>(f)
, std::istream_iterator<int>()
, std::back_inserter(arr)
);
}
int value;
while (std::cin >> value)
std::cout << value << '\n';
In general, stream extractors skip whitespace and then translate the text that follows.
// reading a text file the most simple and straight forward way
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
int main () {
int a[100],i=0,x;
ifstream myfile ("example.txt");
if (myfile.is_open()) // if the file is found and can be opened
{
while ( !myfile.eof() ) //read if it is NOT the end of the file
{
myfile>>a[i++];// read the numbers from the text file...... it will automatically take care of the spaces :-)
}
myfile.close(); // close the stream
}
else cout << "Unable to open file"; // if the file can't be opened
// display the contents
int j=0;
for(j=0;j<i;j++)
{//enter code here
cout<<a[j]<<" ";
}
//getch();
return 0;
}
I have this code that reads from marks.txt file.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main () {
string name,result;
int number1;
ifstream myfile ("marks.txt");
if (myfile.is_open())
{
while ( !myfile.eof() )
{
getline (myfile,name,'\t');
getline (myfile,result,'\t');
stringstream(result) >> number1;
cout << number1;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
with my marks.txt file consists of:
john 20
But when i ran the program. Number1 output is 36. How can i convert result string to int correctly??
Note that you're passing \t (tab character) as the delimiter to getline. Are you sure you are using a tab in your input file? If you use a space or any other character, all the input will go into name and your result will be empty, which will leave number1 undefined. I suspect that's the reason you're getting 36 out of nowhere.