954,113 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trouble With Input From a File

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.

#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();
}
kingcrim05
Newbie Poster
8 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

If you only enter one argument then argv[2] is invalid. The program will only have argv[0] and argv[1].

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

kingcrim05
Newbie Poster
8 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
fin.open("numlist.dat");
	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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Nope, i'm still getting -107374176.000000 for all items

kingcrim05
Newbie Poster
8 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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?

kingcrim05
Newbie Poster
8 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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).

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You