| | |
Trouble With Input From a File
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
I'm getting the same very large number for item and for each element in the array after that for loop is done. Why am i not pulling the 6 numbers from my input file? Thanks.
c Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main(int argc, char *argv[]) { /* Extract value for # of elements in array */ int i; sscanf (argv[2], "%d", &i); // printf("i is %d\n", i); /* Create Input and Output Streams */ ifstream fin; ofstream fout; /* Oprn Input and Output Streams */ fin.open("numlist.dat"); fout.open("numlist.dat"); /* Tests to ensure files opened */ if(fin.fail()) { cerr << "Input did not open\n"; exit(2); } if(fout.fail()) { cerr << "Output file did not open\n"; exit(2); } /* Construct New Array */ float *floatArray= new float[i]; /* Put numbers from list into array */ float item; int j=0; // printf("Put Numbers from list into array\n"); for (j=0; j<i;j++) { fin >> item; printf("item is %f\n", item); floatArray[j]=item; printf("floatArray[%d] is %f\n", j, floatArray[j]); } getchar(); }
0
#4 Oct 27th, 2009
C++ Syntax (Toggle Plain Text)
fin.open("numlist.dat"); fout.open("numlist.dat");
Last edited by Dave Sinkula; Oct 27th, 2009 at 9:22 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
1
#6 Oct 27th, 2009
Just sayin'.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
/* Extract value for # of elements in array */
int i;
sscanf (argv[2], "%d", &i);
// printf("i is %d\n", i);
/* Create Input and Output Streams */
ifstream fin;
ofstream fout;
/* Oprn Input and Output Streams */
fin.open("numlist.dat");
//fout.open("numlist.dat");
/* Tests to ensure files opened */
if(fin.fail())
{
cerr << "Input did not open\n";
exit(2);
}
if(fout.fail())
{
cerr << "Output file did not open\n";
exit(2);
}
/* Construct New Array */
float *floatArray= new float[i];
/* Put numbers from list into array */
float item;
int j=0;
// printf("Put Numbers from list into array\n");
for (j=0; j<i;j++)
{
fin >> item;
printf("item is %f\n", item);
floatArray[j]=item;
printf("floatArray[%d] is %f\n", j, floatArray[j]);
}
}
/* numlist.dat
10 20 30 40 50
*/
/* my output
Debug\cpp.exe file.txt 5
item is 10.000000
floatArray[0] is 10.000000
item is 20.000000
floatArray[1] is 20.000000
item is 30.000000
floatArray[2] is 30.000000
item is 40.000000
floatArray[3] is 40.000000
item is 50.000000
floatArray[4] is 50.000000
*/ Last edited by Dave Sinkula; Oct 27th, 2009 at 9:30 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
-7
#8 Oct 27th, 2009
It doesn't work for you because when you open the file for output ofstream truncates it to zero length, erasing all its contents. Comment out ofstream and its open statement then your program will work (after you add the data back into the file again).
Last edited by Ancient Dragon; Oct 27th, 2009 at 10:17 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Reading input from file (C++)
- Struct: Having trouble reading into my file (C++)
- input from file into class (C++)
- how to place a specific info from input file (C)
Other Threads in the C++ Forum
- Previous Thread: precision issues when printing to cout
- Next Thread: How can I add sound to this??
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






