Trouble With Input From a File

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 8
Reputation: kingcrim05 is an unknown quantity at this point 
Solved Threads: 0
kingcrim05 kingcrim05 is offline Offline
Newbie Poster

Trouble With Input From a File

 
0
  #1
Oct 27th, 2009
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.


  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. /* Extract value for # of elements in array */
  9. int i;
  10. sscanf (argv[2], "%d", &i);
  11. // printf("i is %d\n", i);
  12.  
  13. /* Create Input and Output Streams */
  14. ifstream fin;
  15. ofstream fout;
  16.  
  17. /* Oprn Input and Output Streams */
  18. fin.open("numlist.dat");
  19. fout.open("numlist.dat");
  20.  
  21. /* Tests to ensure files opened */
  22. if(fin.fail())
  23. {
  24. cerr << "Input did not open\n";
  25. exit(2);
  26. }
  27. if(fout.fail())
  28. {
  29. cerr << "Output file did not open\n";
  30. exit(2);
  31. }
  32.  
  33. /* Construct New Array */
  34. float *floatArray= new float[i];
  35.  
  36. /* Put numbers from list into array */
  37. float item;
  38. int j=0;
  39. // printf("Put Numbers from list into array\n");
  40. for (j=0; j<i;j++)
  41. {
  42. fin >> item;
  43. printf("item is %f\n", item);
  44. floatArray[j]=item;
  45. printf("floatArray[%d] is %f\n", j, floatArray[j]);
  46. }
  47. getchar();
  48. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,477
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Oct 27th, 2009
If you only enter one argument then argv[2] is invalid. The program will only have argv[0] and argv[1].
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: kingcrim05 is an unknown quantity at this point 
Solved Threads: 0
kingcrim05 kingcrim05 is offline Offline
Newbie Poster
 
0
  #3
Oct 27th, 2009
There will always be exactly two arguments that are put in on the command line. The first is the name of the input file (which will also be the output file once renamed) and the second is a # that is the # of floats in the input file to be read.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,393
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 244
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #4
Oct 27th, 2009
  1. fin.open("numlist.dat");
  2. fout.open("numlist.dat");
Perhaps first open for input. Then after you've extraced what you want and closed the file, then open the same file for output.
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: kingcrim05 is an unknown quantity at this point 
Solved Threads: 0
kingcrim05 kingcrim05 is offline Offline
Newbie Poster
 
0
  #5
Oct 27th, 2009
Nope, i'm still getting -107374176.000000 for all items
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,393
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 244
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: kingcrim05 is an unknown quantity at this point 
Solved Threads: 0
kingcrim05 kingcrim05 is offline Offline
Newbie Poster
 
0
  #7
Oct 27th, 2009
I'm confused why it would be working for you and not for me then. I'm using Visual Studio 2008 for the first time here so that may be it. Where should i be storing my numlist.dat? i have in my source folder along with my msort.cpp file. Is that the problem?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,477
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC