IO Trouble

Mar 26, 2012 at 5:01am
I'm a college Junior in Comp Sci but I've never messed with C++ before and I'm having some trouble with the IO.
I'm trying to read in a file that looks like: "01 12 19 04 05 12 19", and goes on for about 2000 integers.
My code to do this is:
1
2
3
4
5
6
7
8
9
10
ifstream pages(location); 
while(!pages.eof())
{
	getline (pages, filler);
	istringstream reader(filler);
	while(!reader.eof())
	{
		reader.getline(buff, 2, ' ');
		holder = atoi(buff);
//Other stuff 

The problem seems to be that the int holder is never changing, it's staying as 0. It should be changing from 01 to 12 to 19 to 04, etc.
To me, it looks like maybe the pointer inside of reader is not moving, but I'm not sure if that is right or not, and I'm not sure how to fix it if that is the problem.
Last edited on Mar 26, 2012 at 5:03am
Mar 26, 2012 at 5:09am
here is a function from one of my older files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool inputarray(){
        message.open ("/Users/home/Desktop/cipher/cipher/message.txt");
        message.exceptions(ifstream::badbit);
        try{
            for (int i = 0; i < 50; i++){
                message.get(notcoded[i]);
            }
        }
        catch(ifstream::failure &e){
            cout << "could not open file!";
            message.close();
            return false;
        }
        message.close();
        return true; 
    }

//declared variables im using in the class that didnt show up
char notcoded[50];
    ifstream message;
// im using namespace std in this program, i do not know why... 

that might help you out. the problem i find is that .eof() rarely has its use, and when you know the number of variables, it might be easier to just do it similar to the way i did it. otherwise you can calculate the length using other methods or just use .eof() it is up to you.
Mar 26, 2012 at 5:15am
I'm not sure what is wrong with your code, but I've never actually used an istringstream object before. This is what I would have done (you don't need to use that annoying atoi):

1
2
3
4
5
6
7
8
9
ifstream pages(location); 
while(pages.good())
{
	getline (pages, filler);
	stringstream reader(filler);
	while(reader.good())
	{
		reader >> holder;
//Other stuff  
Last edited on Mar 26, 2012 at 5:20am
Mar 26, 2012 at 5:26am
@Stewbond
Thank you, that seemed to do to the trick. I guess eof should be avoided when possible.
Last edited on Mar 26, 2012 at 5:56am
Mar 26, 2012 at 5:28am
@ui uiho
Thank you for your input. Unfortunately, my professor only gave us reference files to work with. He made them each 2000 integers long, but said he will be using files varying from 1 to 10,000 integers. So, I would not be able to easily hard code the length of the array.
Mar 26, 2012 at 5:44am
Spoke too soon I suppose.
It turns out my string filler is never getting any values from the input file. I'm really confused as to why it's not even reading in the file. It looks, to me at least, like various tutorials I've found.
Here is the entire program :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int numPF=0;
int* pageFrames;
int holder=0;
int temp=0;
int found = 0;
int pageFault = 0;
int wait=0;
string filler;
char* buff;
stringstream reader;

int main()
{
	cout << "Please enter the number of page frames to be allocated for the program: ";
	cin >> numPF;
	buff = new char;
	pageFrames = new int [numPF];
	ifstream pages ("C:\TstRefStr1",ifstream::in);
	getline (pages, filler);
	cout << filler.c_str() << "\n";//for debugging purposes, to validate filler was never getting filled
	getline (pages, filler);//There are 4 lines to the header of the file that can be ignored.
	getline (pages, filler);
	getline (pages, filler);
	while(pages.good())
	{
		getline (pages, filler);cout << filler;
		stringstream reader(filler);
		while(reader.good())
		{
			reader >> holder;
			for(int i=0;i<numPF;i++)
			{
				if(holder == pageFrames[i])
				{
					found = 1;
					for(int j=1;j<numPF;j++)
					{
						if(j == 1)
						{
							temp = pageFrames[i];
							pageFrames[i] = NULL;
						}
						else
						{
							temp = pageFrames[j+1];
							pageFrames[j+1] = pageFrames[j];
							pageFrames[j] = temp;
						}
					}
				}
			}
			if(found == 0)
			{
				pageFault++;
				for(int j=numPF;j>0;j--)
				{
					temp = pageFrames[j-1];
					pageFrames[j+1] = pageFrames[j];
					pageFrames[j] = temp;
				}
				pageFrames[0] = holder;
			}
		}
	}
	pages.close();
	cout << "Number of Page Faults: " << pageFault;
	cout << "\nMethod used: FIFO";
	cin >> wait;
}
Last edited on Mar 26, 2012 at 6:12am
Mar 26, 2012 at 8:01am
You have too many for and while loops and that will make your program hard to understand, especially when is a bug in it. Try to organize that loops sequencially.
Mar 26, 2012 at 10:52am
This "C:\TstRefStr1" is wrong. The backslash starts an escape sequence. Use '\\' in order to get the backslash as a char: "C:\\TstRefStr1"
Mar 26, 2012 at 11:44am
Alternatively, use forward slash. Works on Win and *nix.
"C:/TstRefStr1"
Mar 26, 2012 at 12:07pm
I just have to say this:

but said he will be using files varying from 1 to 10,000 integers


1
2
3
4
5
ifstream inFile("file name");
int data[10000], count =0;
while (inFile >> data[count++])

cout << "The file has " << count << " integers, and they are in my data array.";


Right? Your problem (in class) seems more complicated than just extracting integers though.

Last edited on Mar 26, 2012 at 12:11pm
Mar 26, 2012 at 4:10pm
@LowestOne
True, my problem is more complicated. I'm supposed to write a program, in a language of my choosing (I want to learn C++, so I chose it), that acts as a FIFO Memory Management Policy.
I realize I could just code in 10,000, but the professor has pulled a fast one on me before so I'm trying to make sure it's dynamic. It also consumes fewer resources if I don't force the array to be 10,000 elements long.

I spent some time working with the program last night and got it to read in values and work for the most part. However, when it tries to print the results I get an error of "Windows has triggered a breakpoint in OSLab4.exe" and I am trying to debug that.

I have been able to figure out that my variable pageFault is causing the problem, but I am not at all sure why.
Last edited on Mar 26, 2012 at 4:48pm
Mar 27, 2012 at 3:46am
In case anyone was wondering, I solved my last problem.
The variable buff was creating an error within the heap since I never declared a maximum size for it. Once I did this, everything worked seamlessly.
Topic archived. No new replies allowed.