Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 24th, 2006, 5:36 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
help

could someone have a look at this code and tell me what's wrong here?
when i use visual studio, i get an unhandled exception, right after the program reads and prints the data from clients.dat.

#include <stdio.h>

int main (void)
{
	char *data;
	FILE *file;

	fopen_s (&file, "clients.dat", "r");

	fscanf_s (file, "%s", &data);

	fclose (file);

	printf ("%s\n", &data);

	return 0;
}

thanks.
Dark Flare Knight is offline   Reply With Quote
Old Jun 24th, 2006, 6:05 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
char *data; defines a pointer to a char string. It doesn't declare the string or provide memory for it. That has to be done IN ADDITION. You can't store your girlfriend in your address book, it takes a house or apartment or something. Then you can put the pointer (address) in your book. You still need the house. See the pointer tutorial referenced in my sig. Meanwhile, declare a place for the string and either use the address of the place directly, or make a pointer to it and use that. Here's the deal: you're trying to store the whole damn string in what is, in most current systems, a 4-byte area (required for a pointer). You're most likely putting more than four bytes there and trashing memory. Quick trip off into the weeds for your system to barf. Additionally, the argument in fscanf_s needs to be a pointer to the area to store the stuff in. You currently have a pointer to a pointer. Same deal. Either of these, individually or together, are going to kill you.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 24th, 2006, 6:12 PM   #3
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
I was just going to say that, DaWei . DaWei's right (he always is) You would have to allocate an array for that data AND make sure it's the appropriate size... Why don't people understand pointers? I guess I understood them, thanks to the fact I learned z80 assembly before C.
Dragon_Master is offline   Reply With Quote
Old Jun 24th, 2006, 6:17 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Dragon_Master
DaWei's right (he always is)
Shhhh ..... don't say that, he may start to believe it.
grumpy is offline   Reply With Quote
Old Jun 24th, 2006, 6:20 PM   #5
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
DaWei reminds me of this dude I know named Paul. Execept Paul hates all programming languages execept Ada...
Dragon_Master is offline   Reply With Quote
Old Jun 24th, 2006, 6:45 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
he may start to believe it
No chance. Two ex-wives, four daughters, and a ton of female friends deflate my ego if it shows signs of erupting. They're so busy with that they can't curtail my curmudgeonliness, though.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 24th, 2006, 6:57 PM   #7
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
So, Dawei (I tried not get off topic, but failed) what do you think about Ada?
Dragon_Master is offline   Reply With Quote
Old Jun 24th, 2006, 7:13 PM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
He probably thinks Ada is a nice girl.
grumpy is offline   Reply With Quote
Old Jun 24th, 2006, 7:16 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I was around, doing some stuff for the USPS, when it was initially decided that Ada would be THE language used in governmental projects. I can't speak for others, but in our case it meant use Ada or be effective, take your choice. We didn't use Ada. The other Ada, of course, IS a nice girl, with longer legs than Cobol.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 24th, 2006, 7:27 PM   #10
Dragon_Master
Programmer
 
Dragon_Master's Avatar
 
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3 Dragon_Master is on a distinguished road
What?! Yeah, government seems to like Ada, because of it's security, but it's not used any more... Why? What do they use now?
Dragon_Master is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC