Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   help (http://www.programmingforums.org/showthread.php?t=10519)

Dark Flare Knight Jun 24th, 2006 6:36 PM

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.

DaWei Jun 24th, 2006 7:05 PM

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.

Dragon_Master Jun 24th, 2006 7:12 PM

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.

grumpy Jun 24th, 2006 7:17 PM

Quote:

Originally Posted by Dragon_Master
DaWei's right (he always is)

Shhhh ..... don't say that, he may start to believe it. :)

Dragon_Master Jun 24th, 2006 7:20 PM

DaWei reminds me of this dude I know named Paul. Execept Paul hates all programming languages execept Ada...

DaWei Jun 24th, 2006 7:45 PM

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.

Dragon_Master Jun 24th, 2006 7:57 PM

So, Dawei (I tried not get off topic, but failed) what do you think about Ada?

grumpy Jun 24th, 2006 8:13 PM

He probably thinks Ada is a nice girl.

DaWei Jun 24th, 2006 8:16 PM

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.

Dragon_Master Jun 24th, 2006 8:27 PM

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?


All times are GMT -5. The time now is 8:03 AM.

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