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:
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.
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();
returnfalse;
}
message.close();
returntrue;
}
//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.
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):
@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.
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 :
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.
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.
@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.
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.