Reading from file and making new file

Take original file add one to the numbers in the file and make a new f

Page 1 of 1

10 Replies - 743 Views - Last Post: 30 August 2009 - 02:35 AM Rate Topic: -----

#1 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Reading from file and making new file

Post icon  Posted 29 August 2009 - 11:56 AM

This program is suppose to read from numbers.txt then add 1 to every number and create a new txt file called updatedNumbers.txt with all the new numbers in it. I am lost as to where I went wrong here. Thanks for your input.
If I am way off feel free to say to start over.
#include <string>
#include <fstream>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ofstream;
using std::ifstream;
using std::ios;
using std::string;

int main()
{

	string name = "";
	int num = 0;
	int updatedNum = 0;
	int x = 0;
	int heading = 0;
	int columnHeaders = 0;
	int underLines = 0;


	ifstream inFile;
	inFile.open("numbers.txt", ios::in);

	//determine whether the file was opened
	if (inFile.is_open())
	{
		//read a record
		getline(inFile, name);
		inFile >> num;
		while (inFile.eof())
		{
			//display the record
			cout << name << " " << endl;
			//read another record
			getline(inFile, name);
			inFile >> num;
		}


	inFile.close();
	}
	else
	cout << "The file could not be opened." << endl;


	//create file object and open the file
	ofstream outFile;
	outFile.open("updatedNumbers.txt", ios::out);

	//write the updated numbers to the file
	outFile << heading << endl << endl;
	outFile << columnHeaders << endl;
	outFile << underLines << endl;
	for(int x = 0; x++;);
	outFile << num[x] << endl;
	//end for
	outFile << endl;

	//close the file
	outFile.close();


	return 0;



can not get it to compile to try it out the error I get is :error C2109: subscript requires array or pointer type the error is on line 63 but I can not figure out how to fix this.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from file and making new file

#2 SevenWolf  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 67
  • Joined: 26-August 09

Re: Reading from file and making new file

Posted 29 August 2009 - 12:09 PM

Look Closely Here
 for(int x = 0; x++;);
	outFile << num[x] << endl;




also your using array syntax(subscript Operator) on a variable that is not an array.
int num = 0;



and where is your ending right curly brace for main?

This post has been edited by SevenWolf: 29 August 2009 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Reading from file and making new file

Posted 29 August 2009 - 12:32 PM

There seems to be a few things that aren't quite right.

First as sevenwolf has mentioned, you are using a normal integer like an array. The integer only has enough space to store one value.

The first thing I'd change is the first loop. It looks a little messy, this is how I'd do it.
if (inFile.is_open())
{
	for( int i = 0; !inFile.eof() && i < ARRAY_SIZE; i++ )
	{
		  //read a record
		  getline(inFile, name);
		  
		  //display the record
		  cout << name << " " << endl;
		  inFile >> num[i];
	 }


	 inFile.close();
}


btw ARRAY_SIZE is the size that you make num.

Then below that you are using a for loop to output all the values.
for(int x = 0; x < ARRAY_SIZE; x++) // x declared twice
		outFile << (num[x] + 1) << endl; // you want to add 1 to each number right?


This is how I'd do it, also note the comments. It doesn't really matter that you've declared x twice in terms of compiling but having the random x that you'll never use is slightly pointless.

Also the curly brace at the end is important :) . If you don't understand any of it I can explain in more detail.

Experts please correct me if any of this is wrong!

This post has been edited by nuclearfroggy: 29 August 2009 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Reading from file and making new file

Posted 29 August 2009 - 01:04 PM

Hey you could use this once
using namespace std;


rather then this
using std::cout;
using std::cin;
using std::endl;
using std::ofstream;
using std::ifstream;
using std::ios;
using std::string;



Good luck
Was This Post Helpful? 0
  • +
  • -

#5 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Reading from file and making new file

Posted 29 August 2009 - 01:26 PM

Thanks I know about the namespace command but I was told by my teacher that I will end up with a smaller file size if I only do the ones I need. But I bounce back and fourth using it on my programs.

:)


I think I am just going to start from scratch and retry it again .. Thanks for the help so far :)

This post has been edited by vwyodapink: 29 August 2009 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Reading from file and making new file

Posted 29 August 2009 - 05:51 PM

Thought I had a better way and I think I realised my error on my own lets see. Sorry

This post has been edited by vwyodapink: 29 August 2009 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Reading from file and making new file

Posted 29 August 2009 - 06:00 PM

Ok I thought I figured it out but just solved one issue to now have another issue. Can anyone spot why its not writing to the new file correctly? I am not getting my mess up.

Also does anyone have any tips to figure out errors if everything compiles?
#include <iostream>
#include <fstream>


using namespace std;

void readinfile(void);
void writeoutfile(void);
const int maxx=10;
int numbers[maxx];
int count1;



int main()
{
		
		readinfile();
		writeoutfile();
		return 0;
}

void readinfile(void)
{
   int index;
   ifstream inFile;
   inFile.open("numbers.txt", ios::in);

   if(inFile.is_open())
   {
	   index=0;
		while(inFile.eof() && index < maxx)
		{
				
				inFile>>numbers[index];
				index++;
		}
		inFile.close();
		count1 = index;
   

	}
	else
		cout <<"Unable to open file" << endl;
		cout <<"Press any key to continue..."<<endl;
	  


		
  }

  void writeoutfile(void)
  {
		ofstream bFile;

		bFile.open("updatedNumbers.txt");

		for(int index=0;index<count1;index++)
		{
				bFile<<numbers[index];
				if(index<count1-1)bFile<<endl;
		}
		bFile.close();
  }


Was This Post Helpful? 0
  • +
  • -

#8 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Reading from file and making new file

Posted 29 August 2009 - 06:12 PM

If i get the problem right you just need to do the following
while(inFile.eof() && index < maxx)

should be
while(!inFile.eof() && index < maxx)//so it only will read while is not end of file


and
bFile<<numbers[index];


should be
bFile<<numbers[index]+1;//so that it adds one


Hope i got the problem correctly xD

Good Luck and Good job
Was This Post Helpful? 1
  • +
  • -

#9 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Reading from file and making new file

Posted 29 August 2009 - 06:54 PM

It seems to be working closer now. The things its not doing is transfering all the numbers to the new file its 1 number short.
#include <iostream>
#include <fstream>


using namespace std;

void readinfile(void);
void writeoutfile(void);
const int maxx=10;
int numbers[maxx];
int count1;



int main()
{
		
		readinfile();
		writeoutfile();
		return 0;
}

void readinfile(void)
{
   int index;
   ifstream inFile;
   inFile.open("numbers.txt", ios::in);

   if(inFile.is_open())
   {
	   index=0;
		
		while(!inFile.eof() && index < maxx)//so it only will read while is not end of file
		{
				
				inFile>>numbers[index];
				index++;
		}
		inFile.close();
		count1 = index;
   

	}
	else
		cout <<"Unable to open file" << endl;
		cout <<"Press any key to continue..."<<endl;
	  


		
  }

  void writeoutfile(void)
  {
		ofstream bFile;

		bFile.open("updatedNumbers.txt");

		for(int index=0;index<count1;index++)
		{
				bFile<<numbers[index]+1;//so that it adds one
				if(index<count1-1)bFile<<endl;
		}
		bFile.close();
  }



I thought it could be this line causing it to subtract a number
if(index<count1-1)bFile<<endl;


but changing it to
if(index<count1+1)bFile<<endl;


did not seem to do change anything at all at least it did not change anything I could see. The updatedNumbers.txt file displays numbers 11 -20 instead of 11-21

This post has been edited by vwyodapink: 29 August 2009 - 07:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Reading from file and making new file

Posted 30 August 2009 - 02:09 AM

This is because you are using 11 numbers instead of 10. If you change maxx to 11 you should get correct output. :)

This post has been edited by nuclearfroggy: 30 August 2009 - 02:10 AM

Was This Post Helpful? 1
  • +
  • -

#11 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Reading from file and making new file

Posted 30 August 2009 - 02:35 AM

Wow that was simple enough lol. Thanks for pointing that out I was still lost where I was wrong. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1