Pelles C forum

C language => Beginner questions => Topic started by: Raymond4141 on December 24, 2014, 06:06:24 PM

Title: program controlled input and output files.
Post by: Raymond4141 on December 24, 2014, 06:06:24 PM
I am writing a program controlled file for pelles c and when I run it I get this error message ***processed returned 255***. I am running windows 7 as my os.
Title: Re: program controlled input and output files.
Post by: frankie on December 24, 2014, 08:42:53 PM
Sorry we miss the crystal ball here...  ;D
Post the code.
Merry Xmas
Title: Re: program controlled input and output files.
Post by: Raymond4141 on December 30, 2014, 08:27:12 PM
Sorry bout that,
Here is the code:
      #Include <stdio.h>
      #define KMS_PER_MILE  1.609

int
main (void)
{
    double miles,kms;
     FILE   *inp,*outp;

inp = fopen ("c: distance.txt", "r");
outp = fopen ("c: distout.txt", "w");

fscanf (inp, "%lf", &miles);
fprintf(outp, "The distance in miles is %.2f.\n", miles);

kms = KMS_PER-MILE * miles;

fprintf (outp, "That equals %.2f kilometers.\n", kms);

fclose(inp);
fclose (outp);

return (0);
}


The code when ran crashes and outputs error message ***process returns 255***.
Thanks for any help you can give in advance.
Title: Re: program controlled input and output files.
Post by: frankie on December 30, 2014, 11:24:46 PM
Just for Xmas the solution for an assignement  ;D


#include <stdio.h>
#define KMS_PER_MILE  1.609

int main (void)
{
double miles,kms;
FILE   *inp,*outp;

inp = fopen ("distance.txt", "r");
if (!inp) //Chek if opening was ok!!!!
{
printf("Can't open input file!\nProgram will terminate\n");
return -1;
}

outp = fopen ("distout.txt", "w");
if (!outp) //Chek if opening was ok!!!!
{
printf("Can't open output file!\nProgram will terminate\n");
return -1;
}

fscanf (inp, "%lf", &miles);
fprintf(outp, "The distance in miles is %.2f.\n", miles);

kms = KMS_PER_MILE * miles;

fprintf (outp, "That equals %.2f kilometers.\n", kms);

fclose(inp);
fclose (outp);

return (0);
}


If you really wanto open files on C: root don't leave spaces!
inp = fopen ("c:distance.txt", "r");
Not
inp = fopen ("c: distance.txt", "r");
Title: Re: program controlled input and output files.
Post by: Bitbeisser on December 31, 2014, 02:17:39 AM
Quote from: frankie on December 30, 2014, 11:24:46 PM
Just for Xmas the solution for an assignement  ;D


#include <stdio.h>
#define KMS_PER_MILE  1.609

int main (void)
{
double miles,kms;
FILE   *inp,*outp;

inp = fopen ("distance.txt", "r");
if (!inp) //Chek if opening was ok!!!!
{
printf("Can't open input file!\nProgram will terminate\n");
return -1;
}

outp = fopen ("distout.txt", "w");
if (!outp) //Chek if opening was ok!!!!
{
printf("Can't open output file!\nProgram will terminate\n");
return -1;
}

fscanf (inp, "%lf", &miles);
fprintf(outp, "The distance in miles is %.2f.\n", miles);

kms = KMS_PER_MILE * miles;

fprintf (outp, "That equals %.2f kilometers.\n", kms);

fclose(inp);
fclose (outp);

return (0);
}


If you really wanto open files on C: root don't leave spaces!
inp = fopen ("c:distance.txt", "r");
Not
inp = fopen ("c: distance.txt", "r");
Close but no cigar!
This will (try to) open the file in the current folder on the C: drive, most likely that of the C compiler installation.

To access a file in the root of the C: drive you have to properly use a "\" after the drive designation "C:", so the right instruction would beinp = fopen ("c:\distance.txt", "r");

Ralf  ;)
Title: Re: program controlled input and output files.
Post by: Raymond4141 on December 31, 2014, 03:25:09 AM
I tried all of them and the out put reads as follows:  Can't open input file and program will terminate.
Title: Re: program controlled input and output files.
Post by: jj2007 on December 31, 2014, 03:44:50 AM
Quote from: Bitbeisser on December 31, 2014, 02:17:39 AMTo access a file in the root of the C: drive you have to properly use a "\" after the drive designation "C:", so the right instruction would beinp = fopen ("c:\distance.txt", "r");

Are you sure you get c:\distance.txt with a single backslash?
Title: Re: program controlled input and output files.
Post by: TimoVJL on December 31, 2014, 09:47:11 AM
Previous topic was already here (http://forum.pellesc.de/index.php?topic=6521.msg24300#msg24300) and a stupid question:
Have you that 'distance.txt' anywhere ?

If it is in C: root then this may work too:
inp = fopen ("c:/distance.txt", "r");
Title: Re: program controlled input and output files.
Post by: frankie on December 31, 2014, 09:43:53 PM
He forgot to create the input file  ;)
Title: Re: program controlled input and output files.
Post by: Bitbeisser on January 01, 2015, 02:09:19 AM
Quote from: jj2007 on December 31, 2014, 03:44:50 AM
Quote from: Bitbeisser on December 31, 2014, 02:17:39 AMTo access a file in the root of the C: drive you have to properly use a "\" after the drive designation "C:", so the right instruction would beinp = fopen ("c:\distance.txt", "r");

Are you sure you get c:\distance.txt with a single backslash?
Oops  :-[, of course not. Just had been working all day in Pascal, so I missed that. It needs to be of courseinp = fopen ("c:\\distance.txt", "r");

Ralf
Title: Re: program controlled input and output files.
Post by: Raymond4141 on January 04, 2015, 10:49:24 PM
I found and fixed the problem! All the help I got here was tremendous! The problems was with my computer. My hard drive was corrupted. That's why I was getting the *** processed returned 255***.
Once again thanks everyone for there help! Got a an 85% on my assignment!