»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 07-23-2003, 03:10 PM   #1 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 2
xsaycocie is on a distinguished road
my simple program crashed. Why?

#include <stdio.h>
#define max 100
main()
{
int index, index2; /*counter */
char array1[max];
float array2[max];
FILE *Indata, *Outdata; /* Input and output file pointer */

Indata = fopen ("a:\\tax_data.txt", "r");
Outdata = fopen ("c:\\out.txt", "w");

while ( fscanf (Indata, "%c", &array1[index]) != EOF ) /* read one char */
{
index++;

}

printf ("%c", array1[0]);

while ( fscanf (Indata, "%f", &array2[index2]) != EOF ) /* read one float num */
{
index2++;

}
printf ("%f", array2[0]);


fclose (Indata); /* close file */






printf ("\nPress ENTER to exit\n");
scanf("\%*c");

}

xsaycocie is offline   Reply With Quote
Old 07-23-2003, 03:18 PM   #2 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 2
xsaycocie is on a distinguished road
my text file consist of the following:

s 88000.90
m 69900.34
.
.
S 32000.99
xsaycocie is offline   Reply With Quote
Old 07-27-2003, 12:43 PM   #3 (permalink)
Registered User
 
nukes's Avatar
 
Join Date: Oct 2002
Location: Scotland, UK
Posts: 2,946
nukes is on a distinguished road
Send a message via AIM to nukes Send a message via Yahoo to nukes
What do you mean crashed, did it compile, or hang when you tried to run it?
Why are you opening 2 files?
The way that would run is to continue grabbing chars from the file until the 100th character or EOF, is that what you want? It could lead to the second while{} loop never being executed.
Why are you not assigning an initial value to index?
And finally. C allocates arrays like so, if you define an array:
char array1[10];
the first element would be array1[0] and the last array1[9]
Even if you had gotten all the other things right, this would be a fatal flaw in the logic, causing a buffer overflow, and while it might still run, it would overwrite another piece of data that may be important.
__________________
_____
NuKeS
nukes is offline   Reply With Quote
Reply




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
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 05:12 AM.