![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
![]() |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
DaWei reminds me of this dude I know named Paul. Execept Paul hates all programming languages execept Ada...
|
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
So, Dawei (I tried not get off topic, but failed) what do you think about Ada?
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
He probably thinks Ada is a nice girl.
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
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?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|