News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Problem with code

Started by tech0912, May 30, 2015, 04:09:56 AM

Previous topic - Next topic

tech0912

I'm experimenting with a basic code to set an initial number and store it and a series of sequential numbers to a text file. When I try to run the .exe file, the window sits there open, and doesn't write any numbers to it. The debugger found nothing wrong, so I don't know what I MYSELF am doing wrong. Here's the code (with my name masked in the file location of course)

Robert

The following should print the numbers to your file. It will not print to the console window. Your complaint "doesn't write any numbers to it" is ambiguous. Is "it" the window or the file?

Robert Wishlaw



#include <stdio.h>

int main()

{ int i;
FILE *fw;
char* filename = "C:\\Users\\*****\\Documents\\numbers.txt";

fw = fopen(filename, "w");

if (fw == NULL)
{printf("Could not open file %s",filename);
return 1;
}

for (i=1000000000;i<10000000000;i++) // You had an incorrect semi-colon here
{

fprintf(fw,"%i\n",i); // You omitted the % formatting specifier
}
        fclose(fw);  // You MUST close the file
return(0);
}


henrin

I guess your loop is never executed.
The loop variable is a integer, ie 32-bits.
The initial value is 1e9 that is 0x3B9ACA00 - it'OK
The test value is 1e10 which cannot fit a 32-bit integer.

The calculator of my PC gives an hex 32-bit value 0x2540BE400. The extra high order bits are omitted so that the high limit is lower that the initial value.

I just know that integer operations do very little checks regarding overflow.
I suggest that you to look in the debugger, in asembly mode to see what i is really compared to.

tech0912

Quote from: henrin on May 30, 2015, 10:50:07 PM
I guess your loop is never executed.
The loop variable is a integer, ie 32-bits.
The initial value is 1e9 that is 0x3B9ACA00 - it'OK
The test value is 1e10 which cannot fit a 32-bit integer.

The calculator of my PC gives an hex 32-bit value 0x2540BE400. The extra high order bits are omitted so that the high limit is lower that the initial value.

I just know that integer operations do very little checks regarding overflow.
I suggest that you to look in the debugger, in asembly mode to see what i is really compared to.
So do I need to set the integer as a long integer instead to avoid overflow?

Sent from my LGLS990 using Tapatalk


tech0912

Quote from: Robert on May 30, 2015, 06:46:48 AM
The following should print the numbers to your file. It will not print to the console window. Your complaint "doesn't write any numbers to it" is ambiguous. Is "it" the window or the file?

Robert Wishlaw



#include <stdio.h>

int main()

{ int i;
FILE *fw;
char* filename = "C:\\Users\\*****\\Documents\\numbers.txt";

fw = fopen(filename, "w");

if (fw == NULL)
{printf("Could not open file %s",filename);
return 1;
}

for (i=1000000000;i<10000000000;i++) // You had an incorrect semi-colon here
{

fprintf(fw,"%i\n",i); // You omitted the % formatting specifier
}
        fclose(fw);  // You MUST close the file
return(0);
}


That's weird because the debugger didn't catch that. I'm using the x64 version of Pelles 8.00 in case I didn't mention.

Sent from my LGLS990 using Tapatalk