C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 4th, 2012, 10:15 AM
dagger2011 dagger2011 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 dagger2011 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 21 sec
Reputation Power: 0
Help with fwrite structure please?

Hello. I'm trying to teach myself C from C for dummies. One of the exercises have you creating a "stock" program where you fwrite a structure. The problem I have it that it keeps writing only the first object in the structure and not the following two values. Someone telling me where I'm messing up would be greatly appreciated.

struct stock_data {
char name[30];
float buy_price;
float current_price;
};

void write_info(void)
{
FILE *stocks;
struct stock_data stock;

printf("Enter stock name:");
gets(stock.name);
printf("What did you buy it for? $");
scanf("%f", &stock.buy_price);

stock.current_price = stock.buy_price/11;

stocks = fopen("stock.dat", "a");
if(stocks==NULL)
{
puts("Error opening file");
exit(1);
}

fwrite(&stock, sizeof(stock),1,stocks);
printf("st = %d", st);
fclose(stocks);
puts("Stock added!");
fflush(stdin);
}


This is only part of the program, but the part that I believe my mistake is in. Thanks in advance!

Reply With Quote
  #2  
Old November 4th, 2012, 10:28 AM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
How do you know that your only writing the first structure element?

Also a note on coding in C, be very careful using the gets() function. I would try the fgets() function instead.

Last edited by G4143 : November 4th, 2012 at 10:31 AM.

Reply With Quote
  #3  
Old November 4th, 2012, 11:12 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
1. How do you know you didn't save everything?
The resulting file is NOT a text file - you can't just load it into an editor and read the text. Well you'll see the .name field up to the first \0. After that, it's likely to be largely unprintable characters or seemingly gibberish.

If the file is around 40 bytes long (print sizeof(stock) to find out how many), then the data is there.

Another idea for windows users is to force binary mode ("ab","rb","wb" in the fopen call).

Grab yourself a hex editor (eg HxD ) to see the content of arbitrary files.

> fflush(stdin)
Don't do this -> read this
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #4  
Old November 4th, 2012, 03:36 PM
dagger2011 dagger2011 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 dagger2011 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 21 sec
Reputation Power: 0
The reason I think only only the first item is recorded is because another function in the program is to read it, and it only shows the name of the stock. This is the function that does that.

void read_info(void)
{
FILE *stocks;
struct stock_data stock;
int x;

stocks=fopen("stock.dat", "r");
if(stocks==NULL);
{
puts("No data in file");
return;
}

while(TRUE)
{
x = fread(&stock, sizeof(stock), 1, stocks);
if(x==0) break;
printf("\nStock name: %s\n", stock.name);
printf("Purchased for $%.2f\n", stock.buy_price);
printf("Current price: $%.2f\n", stock.current_price);
}

fclose(stocks);
}


The other reason I think only the name is recorded is because i tried to view the stock.dat file using the following code.

#include<stdio.h>
#include<stdlib.h>

#define BUFSIZE 255

void main(int argc, char *argv[])
{
FILE *viewfile;
char buffer[BUFSIZE+1]; /* storage */

/* check for proper no. of arguments */
if(argc<2)
{
puts("Missing filename!");
puts("\nHere is the format");
puts("VIEW filename");
exit(1);
}

/* Does the file exist? */

viewfile = fopen(argv[1], "r");
if(!viewfile)
{
printf("Error opening \"%s\"\n", argv[1]);
exit(1);
}

/* display the file's guts */

while(fgets(buffer,BUFSIZE,viewfile))
printf("%s",buffer);
fclose(viewfile);
}

When i use that program, the only thing that shows up is the name of the stock, no weird characters or anything afterwards.


I did check sizeof(stock) and it did return a 40 value. But I know that the information is recorded into the structure before the file because I had that information printed to the screen to make sure. I guess I might be missing something else?

Thanks for the help!?!?!

Reply With Quote
  #5  
Old November 4th, 2012, 04:19 PM
G4143 G4143 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 G4143 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 39 m 39 sec
Reputation Power: 1
You realize that this

Code:
while(fgets(buffer,BUFSIZE,viewfile))
printf("%s",buffer);
fclose(viewfile);


Assumes your displaying c-strings and not raw binary data. Try something like below but with the proper error checking on things like fopen.

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

struct stock_data
{
	char name[30];
	float buy_price;
	float current_price;
};

int main(int argc, char**argv)
{
	FILE * fd = NULL;
	struct stock_data my_stk, ans;
	
	strcpy(my_stk.name, "G4143");
	my_stk.buy_price = 13.98;
	my_stk.current_price = 34.98;
	
	fd = fopen("testfile", "w");
	
	fwrite(&my_stk, sizeof(struct stock_data), 1, fd);
	
	fclose(fd);
	
	fd = fopen("testfile", "r");
	
	fread(&ans, sizeof(struct stock_data), 1, fd);
	
	fclose(fd);
	
	fprintf(stdout, "name->%s, b_price->%f, c_price->%f\n", ans.name, ans.buy_price, ans.current_price);
	
	return 0;
}

Last edited by G4143 : November 4th, 2012 at 04:29 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help with fwrite structure please?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap