| | |
Problem in reading the first number from a line in file
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 106
Reputation:
Solved Threads: 0
Hi,
I am trying to read the file (f1.txt) which contains :
3 0
-6 0
4 0
-8 0
...
I wish to read the first number from consecutive 2 lines and process them.
Following is my code:
---------------------------------------------------------------
std::vector<int> row;
std::vector<std::vector<int> > two_node;
int main()
{
string line1, line2;
int writetofile();
ifstream myfile ("f1.txt");
if(myfile.is_open())
{
while(! myfile.eof() )
{
getline (myfile, line1);
getline (myfile, line2);
row.push_back(line[0]);
row.push_back(line[1]);
two_node.push_back(row);
writetofile();
row.clear();
}
myfile.close();
}
else cout << "Unable to open the file \n";
return 0;
}
int writetofile()
{
ofstream file1;
file1.open("f2.imp",ios::app);
file1 << row[0] << " " << row[1] << " " << 0 << "\n";
file1.close();
return 0;
}
-----------------------------------------------------------------
My code is not working. I need the output in f2.imp as :
3 -6 0
4 -8 0
........
Thanks.
I am trying to read the file (f1.txt) which contains :
3 0
-6 0
4 0
-8 0
...
I wish to read the first number from consecutive 2 lines and process them.
Following is my code:
---------------------------------------------------------------
std::vector<int> row;
std::vector<std::vector<int> > two_node;
int main()
{
string line1, line2;
int writetofile();
ifstream myfile ("f1.txt");
if(myfile.is_open())
{
while(! myfile.eof() )
{
getline (myfile, line1);
getline (myfile, line2);
row.push_back(line[0]);
row.push_back(line[1]);
two_node.push_back(row);
writetofile();
row.clear();
}
myfile.close();
}
else cout << "Unable to open the file \n";
return 0;
}
int writetofile()
{
ofstream file1;
file1.open("f2.imp",ios::app);
file1 << row[0] << " " << row[1] << " " << 0 << "\n";
file1.close();
return 0;
}
-----------------------------------------------------------------
My code is not working. I need the output in f2.imp as :
3 -6 0
4 -8 0
........
Thanks.
Last edited by guest7; Aug 10th, 2008 at 6:59 pm.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
3 0
-6 0
•
•
•
•
3 2
-6 1
•
•
•
•
3 -6 0
Anyway, you need to figure out exactly what your data format is and what you want to keep and what you want to throw away. If it is two lines of two integers, you may want to just use the >> operator rather than the getline function. If you use getline, you may need to somehow extract the two integers from the string and throw one of them out. >> doesn't present that problem, nor would there be any potential conversion problems between char and int. I'm not sure you need to use strings or chars at all for this program, but I don't know what it's supposed to do besides what you've posted. You may just want to keep it as all integers.
•
•
Join Date: Mar 2008
Posts: 106
Reputation:
Solved Threads: 0
Hi,
In every line of the file there will be one integer and one zero always. i need to read the first integer and process that integer. I need to read the first integer of consecutive two lines and store them in a 2 Dimensional vector. Could you please provide me with a pseudo code of how to go about it.
Thanks
In every line of the file there will be one integer and one zero always. i need to read the first integer and process that integer. I need to read the first integer of consecutive two lines and store them in a 2 Dimensional vector. Could you please provide me with a pseudo code of how to go about it.
Thanks
>What do the zeroes represent? Data? A line termination flag?
It doesn't matter. The requirement is only to process the first number on each line. Since none of the other numbers on the line aren't being processed, it's a safe assumption that they won't show up in the output.
>My code is not working.
You've got the right idea, it's just a matter of ironing out the details of your logic.
First, this is how Edward would have done it:
If you don't mind some constructive criticism, there are a few issues with your code that need to be addressed.
This isn't a good way to control an input loop, especially when it comes to files. The contents of the file can affect the number of lines you try to read, and that number can be wrong. The canonical example of this is when reading lines and a file ends with a '\n' character. Here's a test program:
Notice how the first loop has an extra line at the end. That's the off-by-one error where the loop runs once more than it should. The only difference between is1 and is2 is a '\n' at the end. In is1, the last character is '\n' and getline terminates by successfully finding a delimiter; is1.eofbit is not set. In is2, the last character isn't '\n' and getline terminates by reaching end-of-file; is2.eofbit is set.
The underlying problem is that the eof() member function doesn't return true unless there's been an extraction attempt that has failed in a way that causes eofbit for the stream object to be set.
It's more accurate if you use the extraction operation itself. They typically return something you can test for success or failure. For example, getline returns the stream object, and in a boolean condition, the stream object can be immediately tested for failure conditions:
Now the correct number of lines are read regardless of what the last character is.
Ed highly recommends checking every single extraction attempt for failure. What happens if there isn't a second line? What happens if there aren't any lines but the eofbit isn't set? Right now you're just ignoring that situation and assuming that both lines always exist.
Another problem here is that line1[0] and line2[0] are characters, not the integers that you want. By pushing them onto the vector you're causing an implicit conversion from the numeric value of the character to integers. For Unicode or ASCII, that means the first two lines will give you 51 and 45 for line1[0] and line2[0], respectively. That's because the numeric value of '3' is 51 and the numeric value of '-' is 45.
What you need to do is get the integer representation of the strings "3" and "-6". Notice that -6 consists of two characters, by the way. A good standard way is to break out the stringstreams as in Edward's examples above.
It doesn't matter. The requirement is only to process the first number on each line. Since none of the other numbers on the line aren't being processed, it's a safe assumption that they won't show up in the output.
>My code is not working.
You've got the right idea, it's just a matter of ironing out the details of your logic.
First, this is how Edward would have done it: C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <stdexcept> #include <string> #include <vector> int main() { using namespace std; try { // Simulate a data file istringstream is("3 0\n-6 0\n4 0\n-8 0"); vector<int> data; string line; // Save the first number on each line while (getline(is, line)) { istringstream numbers(line); int number; // Try to extract the number if (!(numbers >> number)) throw runtime_error("Expected a valid integer starting at '" + line + "'"); data.push_back(number); } // Process the extracted data for (vector<int>::size_type i = 0; i < data.size(); i += 2) { cout << data.at(i); // Assume an odd size isn't a fatal error // and only displays one processed number if (i + 1 < data.size()) cout << ' ' << data.at(i + 1); cout << " 0\n"; } } catch (const exception& ex) { cerr << "Error processing data file: " + string(ex.what()) << '\n'; } }
// Potential error!
while(! myfile.eof() ) C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> int main() { using namespace std; istringstream is1("line 1\nline 2\nline 3\n"); istringstream is2("line 1\nline 2\nline 3"); string line1; string line2; cout << "Reading is1\n"; while (!is1.eof()) { getline(is1, line1); cout << line1 << '\n'; } cout << "Done reading is1\n"; cout << "Reading is2\n"; while (!is2.eof()) { getline(is2, line2); cout << line2 << '\n'; } cout << "Done reading is2\n"; }
The underlying problem is that the eof() member function doesn't return true unless there's been an extraction attempt that has failed in a way that causes eofbit for the stream object to be set.
It's more accurate if you use the extraction operation itself. They typically return something you can test for success or failure. For example, getline returns the stream object, and in a boolean condition, the stream object can be immediately tested for failure conditions:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> int main() { using namespace std; istringstream is1("line 1\nline 2\nline 3\n"); istringstream is2("line 1\nline 2\nline 3"); string line1; string line2; cout << "Reading is1\n"; while (getline(is1, line1)) cout << line1 << '\n'; cout << "Done reading is1\n"; cout << "Reading is2\n"; while (getline(is2, line2)) cout << line2 << '\n'; cout << "Done reading is2\n"; }
// Potential error! getline (myfile, line1); getline (myfile, line2); // Won't do what you expect! row.push_back(line1[0]); // Dies if line1 is empty row.push_back(line2[1]); // Dies if line2 is empty
Another problem here is that line1[0] and line2[0] are characters, not the integers that you want. By pushing them onto the vector you're causing an implicit conversion from the numeric value of the character to integers. For Unicode or ASCII, that means the first two lines will give you 51 and 45 for line1[0] and line2[0], respectively. That's because the numeric value of '3' is 51 and the numeric value of '-' is 45.
What you need to do is get the integer representation of the strings "3" and "-6". Notice that -6 consists of two characters, by the way. A good standard way is to break out the stringstreams as in Edward's examples above.
Last edited by Radical Edward; Aug 10th, 2008 at 8:34 pm.
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- Random Number Problem For Lottery Program (Visual Basic 4 / 5 / 6)
- Reading in a random line from a file (C++)
- C: - reading in from text (C++)
- Problem In using Substring.. (Java)
- Need Help in Reading characters from a text file (C++)
- Please help with File I/O (Java)
- Reading from external data file (C++)
- reading a same input twice from the console (C)
- Help Reading Info in Text File Into an Array (C++)
- problem with convert jumbled text file to unjumbled text file (C)
Other Threads in the C++ Forum
- Previous Thread: Calculate large Factorials
- Next Thread: Sending Text to a Printer
Views: 1272 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






