|
-
March 24th, 2012, 07:25 PM
#1
Having trouble with a readin. Can't read in a matrix into vector of vector of int.
I had posted this on another place, but since the issue right now is reading in, it can go here. Also, I noticed the other place was getting very few replies for anyone that posted there (i.e. low traffic) Not just for me, but few people seemed to be going there in general and the last post before mine was March 9th and it still hadn't been answered. And the last post before that was either also March 9th or was in February.
http://forums.codeguru.com/showthrea...74#post2061574
Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
int size;
vector<vector<int> > nums;
fstream fin;
fin.open(argv[1]);
int temp;
// reader >> temp;
// nums.push_back(temp);
// fin >> temp;
//nums.push_back(temp);
while (fin >> temp)
{
nums.push_back(vector<int>());
while (!fin.peek() == '\n')
{
nums[size].push_back(temp);
}
size++;
}
fin.close();
for (int i =0; i < nums.size(); i++)
{
for (int j =0; j < nums[i].size(); j++)
{
cout << nums[i][j] << " ";
}
cout << "\n";
}
cout << "\n" << nums.size();
}
Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.
I've spent the past few hours on this and am getting nowhere. And I still need to work on the threading part.
Please help.
Last edited by jedipenguin; March 24th, 2012 at 07:28 PM.
-
March 24th, 2012, 08:28 PM
#2
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
Did you use your debugger? I would use the debugger, step through the code and make sure it is doing what you want it to do.
-
March 24th, 2012, 08:32 PM
#3
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
I don't have an IDE. I do have Netbeams but it somehow doesn't have the required C files or something. A Linux thing called Exceed On Demand 8 is all I have. Linux emacs and stuff like that.
I've tried couts and stuff. I find that it's taking in each number and incrementing the size but it's never adding any numbers, only increasing the size of the vector of vectors of int.
numbs[0].size() is 0 still for some odd reason, and it should be 4. I've no clue why it's doing this.
-
March 24th, 2012, 08:33 PM
#4
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
If you can compile this on Linux, then you have access to GDB. That is a debugger. I would Google how to use GDB.
-
March 24th, 2012, 08:41 PM
#5
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
I've no clue how to work GDB. I didn't even know it existed until now.
Also, I know what's going wrong, it's not adding numbers to the vector of vectors. What I don't know is, why isn't it adding numbers. The debugger probably won't tell me that.
I got this when I tried what the help menu told me to do:
Code:
(gdb) run input.txt
`/home/pradcoc/IT383/Project3/2-a/Readin' has changed; re-reading symbols.
Starting program: /home/pradcoc/IT383/Project3/2-a/Readin input.txt
Missing separate debuginfo for /lib/ld-linux.so.2
Try: zypper install -C "debuginfo(build-id)=77c46e45f6efdf89044415840ec59f7b3c2fd041"
Missing separate debuginfo for /usr/lib/libstdc++.so.6
Try: zypper install -C "debuginfo(build-id)=a06c7686acca71405f69e506919c07c1db9ea518"
Missing separate debuginfo for /lib/libm.so.6
Try: zypper install -C "debuginfo(build-id)=293aa876def3cb4177e0389f96c11e76b839b7e4"
Missing separate debuginfo for /lib/libgcc_s.so.1
Try: zypper install -C "debuginfo(build-id)=815f111c78054fb492b3e12dd7bef2d7efc808b7"
Missing separate debuginfo for /lib/libc.so.6
Try: zypper install -C "debuginfo(build-id)=947f93f3895469254f408b3eff732d0f12135a60"
5
No, I can't install files there. It's the school's system and I don't have the rights to add those updates (if it's asking for me to install some files to the thing.) It can only get to it via network. It's not my own system or program.
Last edited by jedipenguin; March 24th, 2012 at 08:46 PM.
-
March 24th, 2012, 08:48 PM
#6
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
The basics of debugging are this:
1. Set breakpoint
2. Run code
3. Step through code from breakpoint.
You just need to figure out how to set breakpoints. Then, you run your program in GDB (you will also need to build your code with the debug flag so that symbols are made).
Debugging is a crucial part of learning to code and learning how to debug will be a tremendous help.
That said, one problem I see with your code is that you never initialize size.
-
March 24th, 2012, 08:51 PM
#7
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
I fixed that. I had assumed that it would auto initialize like primitives do in java.
Even still, I fixed that but that's not it.
I was trying to get it to stop adding to the first vector of vectors and start adding to the second and so on when it reaches the newline.
However, the
while (fin.peek() != '\n')
isn't doing what I wanted. Yet I've no earthly clue what I should use and wish people would just tell me.
-
March 24th, 2012, 08:56 PM
#8
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
What is the input file format? What are you trying to do? Why do you need a 2D vector? Finally, you run GDB with the program name (I would just hard code the file name for now).
-
March 24th, 2012, 09:03 PM
#9
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
Because I'm reading in a matrix and I want it to have both rows and columns but don't know the size ahead of time, and ain't allowed to assume the size ahead of time either. It would be far easier with arrays, but I don't know how to do a dynamic array. Plus, it might lead to memory leaks. I know how to handle those, I think. However, is there a way I could do it with arrays? If so, please specify.
It's a text file. I ran the debugger and it ran and gave me that message above.
I already know that it's my
.peek() thing in the while loop that's causing the glitch. However, I don't know what the proper thing to look for a newline character would be. Reading in a character at a time won't do either. That's too tricky for me with my little skill to put them back together correctly into the proper numbers.
Last edited by jedipenguin; March 24th, 2012 at 09:06 PM.
-
March 24th, 2012, 09:23 PM
#10
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
Ok, lets say I am given this matrix in a text file:
The way I would read this into a 2d array is by reading it a line at a time. I would use this line in an inner loop to create a temporary vector. You then push that vector onto your matrix vector:
Code:
//Assume fin is the file stream
string line;
while( getline(fin, line) )
{
//Now line contains the line, but we want to get the numbers...
istringstream lineStream(line);
//lineStream is now a stream, created from the line, we can use >> normally
int temp;
vector<int> tempVec;
// Use lineStream as if you were reading from a file
}
I just put together the above code snippet. I don't want to post more because then I feel I would be giving this away and I don't know if this is an assignment.
-
March 24th, 2012, 10:21 PM
#11
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
This is an assignment but this isn't a C++ class. It's more an operating systems class.
Also, I've never heard of istringstream before. I was able to read in one line as I said, but it can't figure out a line break. That was my problem.
How to get it to make sure that in
1 2 3 4
5 6 7 8
9 10 11 12
that it puts
1 2 3 4 in the first vector
5 6 7 8 in the another vector
9 10 11 12 in yet another vector
and not putting
1-12 in the same vector.
Or worse yet, not putting anything in the vectors at all.
Last edited by jedipenguin; March 24th, 2012 at 10:28 PM.
-
March 24th, 2012, 10:30 PM
#12
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
You use getline() to grab a line from the file at a time. That line is a string. By creating a stringstream from the string (your line), you now have a stream object. You can use >> to extract each number from the stream. The outer while loop means it will go onto the next line...I don't see the problem here.
-
March 24th, 2012, 10:33 PM
#13
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
What outer while loop?
Also, it's reading in a string. It won't like it if it push a string onto an int vector. What was the function again? atoi()? stoi()?
Do you mean the
while (fin >> temp)
while loop as the outer loop?
Also, won't it be reading in twice then with the fin >> temp and the getline() ?
Last edited by jedipenguin; March 24th, 2012 at 10:35 PM.
-
March 24th, 2012, 10:49 PM
#14
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
Test this code snippet:
Code:
void testfunc()
{
string tst("1 2 3");
istringstream tempStream(tst);
vector<int> tempVec;
int temp;
while( tempStream >> temp )
{
tempVec.push_back(temp);
}
for( int i = 0; i < tempVec.size(); i++ )
{
cout << tempVect[i] << endl;
}
}
I may have made a syntax mistake...but test that function. Don't forget to include <sstream> for stringstreams.
-
March 26th, 2012, 12:51 PM
#15
Re: Having trouble with a readin. Can't read in a matrix into vector of vector of int
Thanks. Now it seems to work. So getline reads just one line until it runs out of lines?
Also, it's not quite working as planned. My vector of vectors should be having a size of 2 yet it's getting a size of 3. I need my sizes right in order to do the other part of this.
Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
int main(int argc, char* argv[])
{
int size = 0;
vector<vector<int> > nums;
fstream fin;
fin.open(argv[1]);
int temp = 0;
// reader >> temp;
// nums.push_back(temp);
// fin >> temp;
//nums.push_back(temp);
string line;
while (getline(fin, line))
{
istringstream tempStream(line);
vector<int> tempVec;
int temp2;
while( tempStream >> temp2 )
{
tempVec.push_back(temp2);
}
nums.push_back(tempVec);
}
fin.close();
for (int i =0; i < nums.size(); i++)
{
for (int j =0; j < nums[i].size(); j++)
{
cout << nums[i][j] << " ";
}
cout << "\n";
}
cout << "\n" << nums.size();
return 0;
}
It's adding a third vector with nothing in it. How come?
Last edited by jedipenguin; March 26th, 2012 at 01:07 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|