| | |
Reading in from file
![]() |
•
•
Join Date: Oct 2006
Posts: 165
Reputation:
Solved Threads: 3
Hey everyone. I have a project to work on but for some reason I cant get my basics to work. Im trying to read in from a file and just printing what I read in from file to an outfile but its not working. The output looks like this:
3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039
This is my code. Its very basic but for some reason not working. Any suggestions? Thanks.
3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039 3.30504e-039
This is my code. Its very basic but for some reason not working. Any suggestions? Thanks.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; int main() { int i=0,j=0; int const MAXSIZE = 100; float data[MAXSIZE]; //Open files for read/write ifstream infile("lab7_input.txt"); ofstream outfile("lab7_output.txt"); for( i = 0; i < MAXSIZE; ++i ) infile >> data[i]; for( j = 0; j < MAXSIZE; ++j ) outfile << data[i] << " " ; return 0; }
Your second loop should use j as a subscript for your array, not i
If you wrote
rather than declaring the variable at the start (in C style), then a newer compiler would have made i go out of scope, and you would have gotten a nice warning about using the i subscript where you shouldn't have.
If you wrote
for( int i = 0; i < MAXSIZE; ++i )rather than declaring the variable at the start (in C style), then a newer compiler would have made i go out of scope, and you would have gotten a nice warning about using the i subscript where you shouldn't have.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
•
•
Join Date: Oct 2006
Posts: 165
Reputation:
Solved Threads: 3
Ok. So I wrote some more code and I got crazy errors. I don't know what to do from here. Any suggestions? Code is below and is well documented to show how it should function.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int main() { int const MAXSIZE = 100, input = 0; float data[MAXSIZE], tmp = 0; //Open files for read/write ifstream infile("lab7_input.txt"); ofstream mergeoutfile("lab7_mergeout.txt"); ofstream bubbleoutfile("lab7_bubbleout.txt"); //Main loop while(input != 4) { //Prompt user with operations cout << endl << "What would you like to do?" << endl; cout << "1: Read data in from file" << endl; cout << "2: Sort the data using Bubblesort" << endl; cout << "3: Sort the data using Mergesort" << endl; cout << "4: Exit" << endl; cin >> input; cout << endl; //Perform selected operation switch(input) { case 1://Read data from file into array "data" for( int i = 0; i <= MAXSIZE-1; ++i ) infile >> data[i]; cout << "The data has been successfully read into the array." << endl; break; case 2://Sort array using Bubblesort for( int j = 0; j <= MAXSIZE-1; ++j ) { if( data[j] > data[j+1] ) { tmp = data[j]; data[j] = data[j+1]; data[j+1] = tmp; } } //Print to outfile "lab07_bubblesort.txt" for( j = 0; j <= MAXSIZE-1; ++j ) bubbleoutfile << data[j] << " " ; //Inform user of successful bubblesort cout << "The data has been sorted using Bubblesort and" << endl; cout << "printed to file lab7_bubbleout.txt." << endl; break; /* case 3://Sort array using Mergesort //Print to outfile "lab07_mergesort.txt" for( int k = 0; k < MAXSIZE; ++k ) bubbleoutfile << data[k] << " " ; //Inform user of successful mergesort cout << "The data has been sorted using Mergesort and" << endl; cout << "printed to file lab7_mergeout.txt." << endl; break; */ case 4: return 0; break; default: cout << "Please pick a number between 1 and 4" << endl; break; } } return 0; }
> int const MAXSIZE = 100, input = 0;
Well you need to declare input separately, it thinks it's supposed to be constant.
Which isn't so good when you try and input.
Also, you're missing some 'int' declarations in your for loops.
Well you need to declare input separately, it thinks it's supposed to be constant.
Which isn't so good when you try and input.
Also, you're missing some 'int' declarations in your for loops.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
![]() |
Similar Threads
- RE: reading .wav file and putting ito inot the array (C)
- Reading .dat file (Visual Basic 4 / 5 / 6)
- Reading an input file as a class memeber function (C++)
- reading txt file into array (C++)
- Not reading in entire file. (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- problem reading text file to struct (C++)
- reading and printing a file to screen in C (C)
Other Threads in the C++ Forum
- Previous Thread: Stripper program!
- Next Thread: help me ffix this
Views: 1425 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error exception file files filestream forms fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework iamthwee input int lazy link linked-list linker list loop looping loops math matrix member memory newbie number object objects opengl output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference search sort spoonfeeding string strings struct student studio template templates text time tree url variable vc++ vector video visual win32 window windows winsock wxwidgets






