File Handling problem.....

there is a problem with the file handling part of my code.....

Page 1 of 1

10 Replies - 2304 Views - Last Post: 19 December 2008 - 01:30 AM Rate Topic: -----

#1 pro-grammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-April 08

File Handling problem.....

Posted 18 December 2008 - 09:27 AM

#include <stdio.h>
#include <string.h>

struct sale
{
  int week;
  int units;
  int price;
  char name[30];
};

File *inFile;
File *outFile;

int main(int argc, char *argv[])
{
  int week = 0;
  int units = 0;
  int price = 0;
  char name[30];
  int count = 13;
  int i = 0;
  struct sale weeklySale[13];
  int total = 0;
  //int total_Sale[13];

	 for (i = 0; i < 13; i++)
	 {
	   scanf("%i %i %i %s", &weeklySale[i].week, &weeklySale[i].units, &weeklySale[i].price, weeklySale[i].name);

	   if(weeklySale[i].week > 13)
	   {
		 break;
	   }

	 }

		 inFile = fopen(argv["data.txt"], "r");
	 outFile = fopen(argv["data.txt"], "w");

	 fprintf(outFile, "%i %i %i %s", &weeklySale[i].week, &weeklySale[i].units, &weeklySale[i].price, weeklySale[i].name);
	 fscanf(inFile, "%i %i %i", &week, &units, &price);

	 int total = &units * &price;

	 printf("Week			   Total Sales \n");
	 printf("%i %i", week, total);


  return 0;
}



this is the code i have so far, i want to save the variables from the struct (weeklySale) into a file (data.txt), then read them back from the same file, perform a calculation with the data that i retrieved, and then display them to the screen.

the problem is in the two lines just above the int main() function and there is also a problem with the code in the main part of the program from the "inFile" command all the way to the last "printf" statement.

can you please check it out and let me know what you think might be causing the problem, it is doing my head in :crazy: . thanks in advance!

This post has been edited by pro-grammer: 18 December 2008 - 09:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: File Handling problem.....

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,541
  • Joined: 23-August 08

Re: File Handling problem.....

Posted 18 December 2008 - 09:36 AM

Did you mean
FILE *inFile;
FILE *outFile;
???

In the future, if you're encountering compilation errors, please post them.
Was This Post Helpful? 0
  • +
  • -

#3 pro-grammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-April 08

Re: File Handling problem.....

Posted 18 December 2008 - 09:53 AM

View PostJackOfAllTrades, on 18 Dec, 2008 - 08:36 AM, said:

Did you mean
FILE *inFile;
FILE *outFile;
???

In the future, if you're encountering compilation errors, please post them.


yes. so, you mean that you want the error messages aswell?
well, i would post them but there are so many..... like, at least 10.....
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: File Handling problem.....

Posted 18 December 2008 - 09:56 AM

 int total = &units * &price;



This made me take pause. You are multiplying two memory addresses together and storing it as an integer.... There are many things wrong with that, but it indicates a lack of understanding in syntax/basic code features which will make it difficult to solve "real" problems.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,541
  • Joined: 23-August 08

Re: File Handling problem.....

Posted 18 December 2008 - 11:10 AM

Good catch, KYA!

Now, moving on...
inFile = fopen(argv["data.txt"], "r");
outFile = fopen(argv["data.txt"], "w");


What are you trying to do there with the argv thing?

You're also trying to read and write to the the file at the same time. When you do this (correctly):
 inFile = fopen(argv["data.txt"], "r");
outFile = fopen(argv["data.txt"], "w");


opening outFile for writing is going to truncate the file at the beginning.

You need to think about what you're trying to do and write it on a piece of paper, because it's not clear to anyone reading your code. What exactly is your assignment?
Was This Post Helpful? 0
  • +
  • -

#6 pro-grammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-April 08

Re: File Handling problem.....

Posted 18 December 2008 - 11:27 AM

View PostKYA, on 18 Dec, 2008 - 08:56 AM, said:

 int total = &units * &price;



This made me take pause. You are multiplying two memory addresses together and storing it as an integer.... There are many things wrong with that, but it indicates a lack of understanding in syntax/basic code features which will make it difficult to solve "real" problems.


I understand what you're saying, and infact, i do have a lack of understand to do with some of the syntax in C programming because it is the first time I am ever using this language.

but, I want to multiply the values of two variables and put them into a single variable and then print out the new value.

for example. i want to do something like the following:

x = 3;
y = 2;

total = x * y;

printf("the total is:", total);  //the number 6 is printed out



so..... how would you suggest i go about it?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: File Handling problem.....

Posted 18 December 2008 - 11:30 AM

That's how you do it.
Was This Post Helpful? 0
  • +
  • -

#8 pro-grammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-April 08

Re: File Handling problem.....

Posted 18 December 2008 - 11:38 AM

View PostJackOfAllTrades, on 18 Dec, 2008 - 10:10 AM, said:

Good catch, KYA!

Now, moving on...
inFile = fopen(argv["data.txt"], "r");
outFile = fopen(argv["data.txt"], "w");


What are you trying to do there with the argv thing?

You're also trying to read and write to the the file at the same time. When you do this (correctly):
 inFile = fopen(argv["data.txt"], "r");
outFile = fopen(argv["data.txt"], "w");


opening outFile for writing is going to truncate the file at the beginning.

You need to think about what you're trying to do and write it on a piece of paper, because it's not clear to anyone reading your code. What exactly is your assignment?


the assignment is to record sales over a period of 13 weeks (and no more).
for sales, we need to enter the week number in which it was sold, the units sold, the price per unit (in pennies) and the name of the product. the user should be able to enter information as many times as they wish, but every time a new sale is added, it should be stored in a file, and the program should stop if anything above the number 12 is entered as a week number. once the program stops, the data stored in the file is read back, and displayed in the form of the week number and total sales (as a double) for each week.

e.g.

if there are 3 entries for week 1 where 2 packs of biscuits were sold, at 30p. then, in the total sales, for week 1 it would say..... "1(under week)
and 1.80(30pence * 2packs= 60pence, 60pence * 3entries = 180pence... under total sales)

hope that makes it clear.
Was This Post Helpful? 0
  • +
  • -

#9 pro-grammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 10-April 08

Re: File Handling problem.....

Posted 18 December 2008 - 04:51 PM

View PostKYA, on 18 Dec, 2008 - 10:30 AM, said:

That's how you do it.


so then I was right, when I did:

fscanf(inFile, "%i %i %i", &week, &units, &price);

int total = &units * &price;

printf("%i", total);



....if I was wrong, what is the difference between the code above and the code below:

x = 3;
y = 2;

total = x * y;

printf("the total is:", total);



my question is..... if I am reading some data from a file..... and I want to store certain values (from the file) in different variables, what is the syntax to do that?
Was This Post Helpful? 0
  • +
  • -

#10 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: File Handling problem.....

Posted 19 December 2008 - 12:40 AM

Pointers: http://www.cplusplus...l/pointers.html
Was This Post Helpful? 0
  • +
  • -

#11 Linkowiezi  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: File Handling problem.....

Posted 19 December 2008 - 01:30 AM

No you can't do it this way:
int total = &units * &price;
But this way:
int total = *units * *price;

'scanf(...)' want's you to use the & because it wishes to manipulate the real value and then need to know the adress of it.

'int total = something' Doesn't want memory adresses but the values stored in the memory.
Like when your friends comes to your house, they probably not want to hang with the house but with you who live in it.
To get that value you use the asterisk '*' in front of the value.
As when you declare a pointer. Yes this can be a bit confusing... but sadly enough, that's just the way things are...
When you get used to using * in the right places you might explain this same thing to someone else ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1