NO

Author Topic: Problem with code  (Read 1633 times)

tech0912

  • Guest
Problem with code
« on: May 30, 2015, 04:09:56 am »
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)

Offline Robert

  • Member
  • *
  • Posts: 182
Re: Problem with code
« Reply #1 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

Code: [Select]

#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);
}


Offline henrin

  • Member
  • *
  • Posts: 21
Re: Problem with code
« Reply #2 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.

tech0912

  • Guest
Re: Problem with code
« Reply #3 on: June 08, 2015, 08:13:24 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

  • Guest
Re: Problem with code
« Reply #4 on: June 08, 2015, 08:14:28 pm »
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

Code: [Select]

#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