I have an assignment where I am supposed to read multiple files containing integers (one on each line) and merge them into a output text file after sorting them. I am new to C++ so I do not know how everything works. I am testing my program with two .txt files. The first file is called fileone.txt, contains 1,2,7 (I do not know how to format this but they are all on different lines.) The second file is called filetwo.txt, and contains 1,3,5,9,10 (again every integer is on a different line).
I have written the following code which opens both files and prints the contents.
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
ifstream iFile;
ifstream jFile;
iFile.open("fileone.txt");
jFile.open("filetwo.txt");
int int1 = 0;
int int2 = 0;
if (iFile.is_open() && jFile.is_open() ){
while (iFile.good() || jFile.good() ) {
iFile >> int1;
jFile >> int2;
cout << "From first file:" << int1 << endl;
cout << "From second file:" << int2 << endl;
}
}
iFile.close();
jFile.close();
return 0;
}
The output of this program is
The problem I am having is the last number in the first file gets printed multiple times. The output I want is to stop printing after printing the last integer from the file. The problem only appears when the second file contains more integers than the first one. Is there a way to stop printing from the first file when it reaches the end and while still print all the numbers from the second file?
This will do the trick
while (iFile || jFile) {
if(iFile >> int1) // make sure the read succeeded!!
cout << "From first file:" << int1 << endl;
if(jFile >> int2) // make sure the read succeeded!!
cout << "From second file:" << int2 << endl;
}
You should only really use data if you checked to see if it was successfully read.
Consider changing the while loop as follow
while (iFile.good() || jFile.good() ) {
iFile >> int1;
jFile >> int2;
int c = iFile.peek();
int d = jFile.peek();
if (c == EOF) {
if (!iFile.eof())
cout << "From first file:" << int1 << endl;
}
if (d == EOF) {
if (!jFile.eof())
cout << "From second file:" << int2 << endl;
}
}
The thing is to check the end of file and handle if to print it. You can use eof() function as above.
I haven't checked the code. But the logic should be correct.
Related
There's a series of coordinates I'm trying to write to an array so I can perform calculations on, but I haven't been able to read the file correctly since I can't ignore the headers, and when I do remove the headers it also doesn't seem to correctly write the values to the array.
The coordinate file is a txt as below.
Coordinates of 4 points
x y z
-0.06325 0.0359793 0.0420873
-0.06275 0.0360343 0.0425949
-0.0645 0.0365101 0.0404362
-0.064 0.0366195 0.0414512
Any help with the code is much appreciated. I've tried using .ignore to skip the two header lines but they don't seem to work as expected.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main() {
int i = 1;
int count = 1;
char separator;
const int MAX = 10000;
int x[MAX];
int y[MAX];
int z[MAX];
int dist[MAX];
char in_file[16]; // File name string of 16 characters long
char out_file[16];
ifstream in_stream;
ofstream out_stream;
out_stream << setiosflags(ios::left); // Use IO Manipulators to set output to align left
cout << "This program reads a series of values from a given file, saves them into an array and performs calculations." << endl << endl;
// User data input
cout << "Enter the input in_file name: \n";
cin >> in_file;
cout << endl;
in_stream.open(in_file, ios::_Nocreate);
cout << "Enter the output file name: \n";
cin >> out_file;
cout << endl;
out_stream.open(out_file);
// While loop in case in_file does not exist / cannot be opened
while (in_stream.fail()) {
cout << "Error opening '" << in_file << "'\n";
cout << "Enter the input in_file name: ";
cin >> in_file;
in_stream.clear();
in_stream.open(in_file, ios::_Nocreate);
}
while (in_stream.good) {
in_stream.ignore(256, '\n');
in_stream.ignore(256, '\n');
in_stream >> x[i] >> separator >>y[i] >> separator >> z[i];
i++;
count = count + 1;
}
cout << x[1] << y[1] << z[1];
in_stream.close();
out_stream.close();
return 0;
}
Within your reading of the file, you are using in_stream.ignore(256, '\n'); correctly, but you want to use it outside the while loop. When you have it inside the while loop, every time it runs, you will ignore the first two lines, then read the third. Your output would actually read in only a third of what you expect. To fix this, just move those 2 lines outside the while loop.
in_stream.ignore(256, '\n');
in_stream.ignore(256, '\n');
while (in_stream.good)
{
in_stream >> x[i] >> separator >>y[i] >> separator >> z[i];
i++;
count = count + 1;
}
This should fix your problem, but you should generally use a vector instead of an array. Vectors automatically manage memory and check for bounds instead of you having to do that.
Also, good practice is to read values out of the stream as the while condition instead of in_stream.good:
while(stream >> var)
{
//Your code here
}
Here is a good resource on why that is.
I have been having some problems with my code. I was asked to input elements from an .dat file into an array. For class we have to do this for various files without knowing how many elements will be in each file. The only thing we know is that here will never be more then 5000 elements per file.
One of my input file has the following elements:
5.675207 -0.571210
0.728926 0.666069
2.290909 0.751731 2.004545 0.907396
0.702893 0.646427 5.909504 -0.365045
2.082645 0.871841 5.597107 -0.633507
6.117769 -0.164663 6.091736 -0.190282
5.571074 -0.653433 4.503719 -0.978307
3.983058 -0.745620
3.670661 -0.504729
5.857438 -0.413001
When I run my code:
#define _CRT_NONSTDC_NO_DEPRECATE
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(int argc, char * argv[])
{
ifstream fin;
ofstream fout;
if (argc < 3)
{
cout << "Incorrect usage: prog.exe filenname number" << endl;
cout << "Exiting now, please try again." << endl;
return -1;
}
fin.open(argv[1]);
if (!fin)
{
cout << "Error opening file \"" << argv[1] << "\", exiting." << endl;
return -1;
}
fout.open(argv[2]);
int count = 0;
int word;
double points[5000];
while (fin >> word)
{
fin >> points[count];
++count;
}
fout << "Numer of points is: " << count/2 << endl;
for (int i = 0; i < count; i++)
{
fout << points[i] << " ";
}
fin.close();
fout.close();
return 0;
}
I outputted the elements just to make sure that they were properly inputted. I get the following and I don't know why.
0.675207 0.57121
0.728926 0.666069
0.290909 0.751731 0.004545 0.907396
0.702893 0.646427 0.909504 0.365045
0.082645 0.871841 0.597107 0.633507
0.117769 0.164663 0.091736 0.190282
0.571074 0.653433 0.503719 0.978307
0.983058 0.74562
0.670661 0.504729
0.857438 0.413001
The first digit is converted to a 0 for some reason and the negative ones become positive. Would anyone know why this is occurring?
int word;
is doing you no favours. First it's an integer so fin >> word only reads the integer portion of the inputs. 5.675207 is read as 5. the .675207 is left in the file stream for fin >> points[count]. Words isn't stored anywhere to the 5 is discarded but the .675207 lives on as 0.675207 in points[0].
Where the negative signs are going I didn't bother trying to figure out because
while (fin >> points[count])
{
++count;
}
fixes everything.
When you read in the numbers from the the file you are extracting them as "word" and then storing them as "points". "word" is an integer and "points" is a double, this will give you unexpected behavior. The compiler should give you warnings about this.
I need to find a string (link name) input by the user in a text file.
How can approach a solution in c++? Do I have to store the file context in structs in order to read the data later? Or can I just open and read the file whenever i want to look for info?
Thank you!
Input file sample
111.176.4.191 www.yahoo.com 01/04/2013
111.176.4.191 www.yahoo.com 01/09/2013
192.168.1.101 www.yahoo.com 01/04/2013
111.176.4.191 www.yahoo.com 01/12/2013
192.168.1.101 www.espn.com 01/05/2013
C++ code
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <string>
using namespace std;
//gobal variables, procedures
void fileinfo1(string);
char IP_Address [12];
char Link_Name [50];
char Date_Accessed [8];
string filename;
int menu;
int main()
{
// the user will input the file name here
cout << "Enter filename> ";
getline( cin, filename );
fstream file( filename.c_str() );
if (!file)
{
cout << "Invalid file.\n";
return EXIT_FAILURE;
}
// the program will display the file context
else
{
string line;
int count = 10;
while ((count > 0) && getline( file, line ))
{
cout << line << '\n';
count--;
}
file.close();
}
return EXIT_SUCCESS;
// The user will be able to choose to see info about all entries or a particular one
cout << "Please select a menu option:";
cout << "1)Link Information in date range";
cout << "2)Information about all links";
cout << "3)Quit the program";
cin >> menu;
switch (menu) {
// see info about a particular link
case 1: fileinfo1(filename);
break;
case 2:
break;
case 3:
break;
default: cout << "Please a choose a number between 1 and 3";
break;
}
// the file is passed to this function
void fileinfo1(string filename) {
//the user will input a link e.g www.espn.com
cout << "What is the link name? ";
cin >> Link_Name;
// and also input date range (start-end)
cout << "What is the starting date? " ;
cin >> Date_Accessed;
cout << "What is the ending date? " ;
cin >> Date_Accessed;
// Now, here's where I'm having trouble
// I need to find the wwww.espn.com in my file based on the range date , so that i will be able to increment the number of hits
unsigned int curLine = 0;
while (getline(filename, line)) { // I changed this, see below
curLine++;
if (line.find(search, 0) != string::npos) {
cout << "found: " << search << "line: " << curLine << endl;
}
}
}
}
Thank you!
This Part of the code shouldnt be written into your main() function.
// the file is passed to this function
void fileinfo1(string filename) {
//the user will input a link e.g www.espn.com
cout << "What is the link name? ";
cin >> Link_Name;
// and also input date range (start-end)
cout << "What is the starting date? " ;
cin >> Date_Accessed;
cout << "What is the ending date? " ;
cin >> Date_Accessed;
// Now, here's where I'm having trouble
// I need to find the wwww.espn.com in my file based on the range date , so that i will be able to increment the number of hits
unsigned int curLine = 0;
while (getline(filename, line)) { // I changed this, see below
curLine++;
if (line.find(search, 0) != string::npos) {
cout << "found: " << search << "line: " << curLine << endl;
}
}
}
and you are using way to many global variables which are really not necessary. And you dident declare the variables line and search. This shoudnt even compile.
Do you want a quick and dirty solution or an elegant one?
For an elegant solution, I would:
Ditch the globals.
Read the entire file into memory before parsing it.
Generate an internal database for your data.
Write a few query functions that return a subset of your data.
For your particular case, you could use a std::multimap < LinkName, DateAndIP > to find all data relating to the link. DateAndIP could be a typedef to std::multimap < Date, IP > . If you've never used multimap, this will be a good learning experience. Write your compare functions and use the find member function to return only what you're looking for.
Good luck and happy coding!
I know, this question has been treated tons of times.. but I can't make it work anyway.. here I paste some code:
#include <sstream>
#include "header.h"
using namespace std;
using namespace header;
int main(){
string parent, child, line, node;
int choose = 0;
tree_ptr tree_inst = new tree;
cout << "*** Start ***" << endl;
while (getline(cin, line, '\n')){
istringstream line_stream(line);
line_stream >> parent;
if (parent.compare("0") == 0) break;
while (line_stream >> child) {
if (!tree_inst->insert(parent, child)) {
cout << "*** ERROR! ***" << endl;
tree_inst->visit(tree_inst->root);
cout << "*** End ***" << endl;
return -1;
}
}
}
while (true) {
cin >> choose; //<== doesn't wait for the input, just loop forever
// on the default statement of the switch...
switch (choose) {
...
}
}
}
I've already tried to insert some cin.sync() cin.clear() cin.ignore() ..etc.. but nothing changed!
The first getline() loop would loop forever, if you don't break it in one way or another. If done in a clean manner, this shouldn't be an issue at all. In fact I could not reproduce your error.
How to analyze the error
So the cause is either a bad sate of cin or non numberic non space input that is still pending. To help you find out, I suggest you add some diagnostic code:
cout << "Enter free lines of text or stop to return to the menu:";
while (getline(cin, line, '\n')) { // This would loop foreved
if (line == "stop") // I added this one to exit
break; // but how do you do ?
}
while (true) { // I get no problem here
cout << "Cin status: failed=" // <===Add this simple diagnostic to
<< cin.fail() << " bad=" // check the state of the stream
<< cin.bad() << " eof=" << cin.eof() << endl;
cout << "Next char in decimal is:" // <=== Add this to show
<< cin.peek() << endl; // where in the stream you're hanging
cout << "Choose:";
cin >> choose;
cout << "selected: " << choose << endl;
}
If your stream state is not clean, despite a cin.clean() , it's because either you closed the stream or typed in an end-of-file code at the console (Ctrl+D or Ctrl+Z depending on system) .
It the stream state is clean but the peeked char is not numeric (i.e. decimal code not between 48 and 57), it's the bad input that sets the stream in a fail state.
Edit: your specific case
Looking at the diagnostic you've provided (fail=1, eof=1), it appears that after the first loop you've already eached the end of the input. So your input fails because there is no further data to read. Your input file on pastebin confirms that the last line is the "0" that you use to exit the first loop.
Following our exchanges, I understand that you've in fact redirected input from the command line (e.g. yourprogramme <yourinput.txt ) and expect your code to switch back to keyboard input once the redirected file input reaches its end. Unfortunately this is not the way it works. If you redirect input from a file, cin will always refer to the redireted file.
To be able to mix file and keyboard input, you need to use <fstream> and more precisely ifstream for reading from the file and keep cin for keyboard entry.
Here is a slightly modified code that takes the filename from the command line but without indirection (e.g. yourprogramme yourinput.txt ):
...
int main(int argc, char**argv)
{
if (argc != 2) { // if called from command line wrong arguments
cerr << "You must provide the name of the data file as command line argument\n";
return EXIT_FAILURE;
}
ifstream file(argv[1]); // open the file for reading
if (!file) { // if open failed, end the programme
cerr << "Could not open "<<argv[1]<<"\n";
return EXIT_FAILURE;
}
string line; // here your code from before
int choose = 0;
cout << "*** Loading "<<argv[1]<< " ***" << endl;
// but in the first loop replace cin with file
while (getline(file, line, '\n')){
cout << "Read: " << line<<endl;
if (line == "0") // Abridged version;-)
break;
}
file.close(); // File is no longer needed here.
while (true) { // Second loop, unchanged, using cin
cout << "Choose (9 to exit):";
if (!(cin >> choose)) // avoid looping forever if problem on cin
break;
cout << "selected: " << choose << endl;
if (choose == 9)
break;
}
return EXIT_SUCCESS;
}
sorry for the noob question, but I'm new to C++.
I need to read some information, line-by-line, from a file, and perform some calculations, and output into another file. For example, we read a unique ID for each line, a name, and 2 numbers. The last 2 numbers are multiplied, and in the output file, the ID, name and product are printed line by line:
input.txt:
2431 John Doe 2000 5
9856 Jane Doe 1800 2
4029 Jack Siu 3000 10
output.txt:
ID Name Total
2431 John Doe 10000
9856 Jane Doe 3600
4029 Jack Siu 30000
My code is similar to this, but only the first line appears in the output file. If I press Enter repeatedly, the other lines appear in the output file:
#include <fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
int main () {
int ID, I, J;
string First, Last;
char c;
cout << "ID\tName\t\Total\n";
while ((c = getchar()) != EOF) {
cin >> ID >> First >> Last >> I >> J;
cout << ID << " " << First << " " << Last << " " I * J << "\n";
}
return 0;
}
That's my only problem, that the values don't appear in the output file, unless I press Enter repeatedly, then close the program. Can anyone suggest a fix for my code above, to have it do the task without keyboard input? Thanks!
Use
while (!cin.eof()) {
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
You've hidden the real std::cin and std::cout...and will later read from them.
while ((c = getchar()) != EOF) {
But here you use the real std::cin to check for EOF.
The getchar() call reads waits for you to type a character (and press Enter) since it reads from stdin (standard input). Try changing the loop condition to stop reading when cin reaches end of file.
EDIT
You should also use different names for input and output streams -- there are already cin and cout in the std namespace.
This is because you used getchar() in your while loop condition. Not sure what you were trying to do, but getchar() reads a char from stdin. What you should have done, is check if cin failed or encountered EOF.
While I was looking for the answer I though I better check and make sure it worked. I got some build errors and got a little carried away from there.
Hope this helps!
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream indata("input.txt");
if(!indata)
{ // file couldn't be opened
cerr << "Error: input.txt could not be opened" << endl;
exit(1);
}
ofstream output("output.txt");
if(!output)
{ // file couldn't be opened
cerr << "Error: output.txt could not be opened" << endl;
exit(1);
}
int ID, I, J;
char First[10], Last[10];
output << "ID\tName\tTotal\n";
while (!indata.eof())
{
indata >> ID >> First >> Last >> I >> J;
output << ID << " " << First << " " << Last << " " << I * J << endl;
}
indata.close();
output.close();
return 0;
}