6 Replies - 3613 Views - Last Post: 19 December 2008 - 06:01 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

i am having a problem writing to a file

Posted 18 December 2008 - 12:33 PM

here is my code so far:

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

struct sale
{
  int week;
  int units;
  int price;
  char name[30];
};
	   
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;

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

	  FILE *fp;

	  fp = fopen("data.txt", "wb");
	  if(fp == NULL)
	  {
		 printf("Sorry, there is no such file as data.txt");
	  }

	  fwrite(weeklySale[i].week, weeklySale[i].units, weeklySale[i].price, weeklySale[i].name, fp);

	   fclose(fp) == 0? "succeeded" : "failed";


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

	 }

  return 0;
}



starting from the first for loop, i want to allow users to enter sales information for a period of 13 weeks, and store the sales values in a struct. once the values are in a struct, i want to store the struct values in a file called data.txt. (because data.txt will store all of the struct values whereas the struct will keep getting refreshed)..... then i want to close the "write mode" for the file and open the file in "read mode". once i have done that, i want to output the data in the form of week numbers and total sales in each week.

the problem in my code is that it wont let me write to the file, and the error messages i am getting are the following:

"In function main"
"warning: passing argument 1 of fwrite makes pointer from integer without a cast"
"warning passing argument 4 of fwrite from incompatible pointer type"
"too many arguments to function fwrite"

......does anyone have any idea of whats going wrong because i'm going to kill someone soon if i dont start making some sort of progress :fool: (i've been doing this since yesterday - i know, its very bad!!)

Is This A Good Question/Topic? 0
  • +

Replies To: i am having a problem writing to a file

#2 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: i am having a problem writing to a file

Posted 18 December 2008 - 01:15 PM

the syntax you've used for fwrite is incorrect.
this is the prototype for fwrite :
size_t fwrite(
   const void *buffer,
   size_t size,
   size_t count,
   FILE *stream 
);


buffer
Pointer to data to be written.

size
Item size in bytes.

count
Maximum number of items to be written.

stream
Pointer to FILE structure.

This function is to write to a determined size.(It is appropriate for creating random access files), so if you like to create a sequential access file you may use fprintf otherwise write them individually. if you can use c++ try ofstream class. It's really better.

----------------------------------
if it was helpful click the link below :v:

This post has been edited by amir_ju: 18 December 2008 - 01:32 PM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,435
  • Joined: 23-August 08

Re: i am having a problem writing to a file

Posted 18 December 2008 - 01:33 PM

You're just not thinking about this logically.

Unless your assignment is more involved than you've told us previously, you don't even need a struct, just an array of 13 integer representing the 13 weeks.

Step 1:
Open a file for writing in text mode.

Step 2:
Get input from user until a week greater than 13 is entered. Write the input to the file if the week is < 14, else close the file.

Step 3:
Make sure your array of 13 integers is initialized to all 0s (hint: int weeklySales[13] = { 0 };)

Step 4:
Open the previously written file for reading in text mode. While you can read a line from the text file, add the number of units sold * price to whatever is in array entry week - 1.

Step 5:
Close the file.

Step 6:
Go through each element of your array and print the week and the sales number / 100.

That should do it.

This post has been edited by JackOfAllTrades: 18 December 2008 - 01:35 PM

Was This Post Helpful? 1
  • +
  • -

#4 pro-grammer  Icon User is offline

  • New D.I.C Head

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

Re: i am having a problem writing to a file

Posted 18 December 2008 - 05:41 PM

View PostJackOfAllTrades, on 18 Dec, 2008 - 12:33 PM, said:

You're just not thinking about this logically.

Unless your assignment is more involved than you've told us previously, you don't even need a struct, just an array of 13 integer representing the 13 weeks.

Step 1:
Open a file for writing in text mode.

Step 2:
Get input from user until a week greater than 13 is entered. Write the input to the file if the week is < 14, else close the file.

Step 3:
Make sure your array of 13 integers is initialized to all 0s (hint: int weeklySales[13] = { 0 };)

Step 4:
Open the previously written file for reading in text mode. While you can read a line from the text file, add the number of units sold * price to whatever is in array entry week - 1.

Step 5:
Close the file.

Step 6:
Go through each element of your array and print the week and the sales number / 100.

That should do it.


well actually, I totally agree with you, what you said is much easier but we have been asked to complete the assignment using a struct for collecting the sales for each week..... that's why I was using a struct.
Was This Post Helpful? 0
  • +
  • -

#5 yunusabd  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 104
  • Joined: 25-October 08

Re: i am having a problem writing to a file

Posted 18 December 2008 - 11:24 PM

fp = fopen("data.txt", "wb");


I am just curious whats the b for?
Where did you get the fwrite from?

I've never used either and am too lazy to google :D


Does your Code do what you want it to do?
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,889
  • Joined: 10-May 07

Re: i am having a problem writing to a file

Posted 18 December 2008 - 11:27 PM

View Postyunusabd, on 19 Dec, 2008 - 12:24 AM, said:

fp = fopen("data.txt", "wb");


I am just curious whats the b for?

The b sets fopen to open the file as a Binary File. Quit being lazy & look it up :)
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,435
  • Joined: 23-August 08

Re: i am having a problem writing to a file

Posted 19 December 2008 - 06:01 AM

View Postpro-grammer, on 18 Dec, 2008 - 07:41 PM, said:

well actually, I totally agree with you, what you said is much easier but we have been asked to complete the assignment using a struct for collecting the sales for each week..... that's why I was using a struct.


OK, fine. That's what I meant about other details of the assignment. In that case, you just want a single struct (not an array!) in step 2 to receive the data and write it out to the file.

The reason you don't want an array is that you don't know how many entries a person is going to make, and an array limits you to the number of entries possible. There are ways around this, like a linked list or dynamic memory allocation, but I suspect that is beyond the scope of your assignment at this point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1