A newbie needs help with file processing 
Author Message
 A newbie needs help with file processing

Hello all,
I am learning C now and I am confused about something.

The code that follows works fine with Unix, but when I compile it on a Win95
machine using Borland I get a fault error and my machine crashes.  Since
htis is more than an academic excercise (eventually when I learn more, I
hope to make a program of this nature for my job), I would really like to
know what's causing the instability.  Is there a another method of doing
this pogram that is more stable in Windows?  Is there just a big snake of a
mistake (as in "its so plain that if it were a snake..." in the code that
I'm missing and unix/cc is somehow compensating for?  I use SuSE Linux at
home and Win98 and have found similar instances (though usually of gcc
returning errors at compile or linux safely close the program, and more
importantly giving some clue why).

Thank you
Ben

#include <stdio.h>

struct record {
 char fname[25];
 char lname[25];
 char address1[30];
 char ref1[20];
 char city[30];
 char state[3];
 char zip[10];
 };

main()
{
 struct record label[5];
 int count;
 FILE *labelPtr;

 if ((labelPtr = fopen("data.dat", "w")) == NULL)
   printf("File can not be opened");
 else {
  for (count = 0; count < 5; count++) {
   printf("Enter First Name\n");
   scanf("%s", label[count].fname);
   printf("Enter Last Name\n");
   scanf("%s", label[count].lname);
   printf("Enter Address Line 1\n");
   scanf("%s", label[count].address1);
   printf("Enter Adress Line 2\n");
   scanf("%s", label[count].ref1);
   printf("Enter City\n");
   scanf("%s", label[count].city);
   printf("Enter State (2 character Postal format)\n");
   scanf("%s", label[count].state);
   printf("Enter Zip Code (5 digit only please\n");
   scanf("%s", label[count].zip);

   fprintf(labelPtr, "%s, ", label[count].fname);
   fprintf(labelPtr, "%s, ", label[count].lname);
   fprintf(labelPtr, "%s, ", label[count].address1);
   fprintf(labelPtr, "%s, ", label[count].ref1);
   fprintf(labelPtr, "%s, ", label[count].city);
   fprintf(labelPtr, "%s, ", label[count].state);
   fprintf(labelPtr, "%s, ", label[count].zip);
   fprintf(labelPtr, "%c", '\n');
  }

  }
  fclose(labelPtr);
  return 0;

Quote:
}

--



Mon, 08 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
If the file does not exist you try to close a none existing file which
under dos or win is not forgivven.! Furthermore you have to open the file
with "W+" because only a "W" deletes every time the existing file and you
lose your previus data.
Greetings Anjo.



Quote:
> Hello all,
> I am learning C now and I am confused about something.

> The code that follows works fine with Unix, but when I compile it on a
Win95
> machine using Borland I get a fault error and my machine crashes.  Since
> htis is more than an academic excercise (eventually when I learn more, I
> hope to make a program of this nature for my job), I would really like to
> know what's causing the instability.  Is there a another method of doing
> this pogram that is more stable in Windows?  Is there just a big snake of
a
> mistake (as in "its so plain that if it were a snake..." in the code that
> I'm missing and unix/cc is somehow compensating for?  I use SuSE Linux at
> home and Win98 and have found similar instances (though usually of gcc
> returning errors at compile or linux safely close the program, and more
> importantly giving some clue why).

> Thank you
> Ben

> #include <stdio.h>

> struct record {
>  char fname[25];
>  char lname[25];
>  char address1[30];
>  char ref1[20];
>  char city[30];
>  char state[3];
>  char zip[10];
>  };

> main()
> {
>  struct record label[5];
>  int count;
>  FILE *labelPtr;

>  if ((labelPtr = fopen("data.dat", "w")) == NULL)
>    printf("File can not be opened");
>  else {
>   for (count = 0; count < 5; count++) {
>    printf("Enter First Name\n");
>    scanf("%s", label[count].fname);
>    printf("Enter Last Name\n");
>    scanf("%s", label[count].lname);
>    printf("Enter Address Line 1\n");
>    scanf("%s", label[count].address1);
>    printf("Enter Adress Line 2\n");
>    scanf("%s", label[count].ref1);
>    printf("Enter City\n");
>    scanf("%s", label[count].city);
>    printf("Enter State (2 character Postal format)\n");
>    scanf("%s", label[count].state);
>    printf("Enter Zip Code (5 digit only please\n");
>    scanf("%s", label[count].zip);

>    fprintf(labelPtr, "%s, ", label[count].fname);
>    fprintf(labelPtr, "%s, ", label[count].lname);
>    fprintf(labelPtr, "%s, ", label[count].address1);
>    fprintf(labelPtr, "%s, ", label[count].ref1);
>    fprintf(labelPtr, "%s, ", label[count].city);
>    fprintf(labelPtr, "%s, ", label[count].state);
>    fprintf(labelPtr, "%s, ", label[count].zip);
>    fprintf(labelPtr, "%c", '\n');
>   }

>   }
>   fclose(labelPtr);
>   return 0;
> }

> --


--



Tue, 09 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
Ben,

I checked and compiled your code using my commandline compiler (lcc) and it
worked fine and dandy, I can only say it is probably an issue with your
Borland IDE and Windows throwinga  wobbly at each other, I used to have
similair issues with Borland C++ 4.5 and Borland C++ Builder (which by the
way is the sexiest for Win apps...)

Lee.

Quote:

>Hello all,
>I am learning C now and I am confused about something.

>The code that follows works fine with Unix, but when I compile it on a
Win95
>machine using Borland I get a fault error and my machine crashes.  Since
>htis is more than an academic excercise (eventually when I learn more, I
>hope to make a program of this nature for my job), I would really like to
>know what's causing the instability.  Is there a another method of doing
>this pogram that is more stable in Windows?  Is there just a big snake of a
>mistake (as in "its so plain that if it were a snake..." in the code that
>I'm missing and unix/cc is somehow compensating for?  I use SuSE Linux at
>home and Win98 and have found similar instances (though usually of gcc
>returning errors at compile or linux safely close the program, and more
>importantly giving some clue why).

>Thank you
>Ben

>#include <stdio.h>

>struct record {
> char fname[25];
> char lname[25];
> char address1[30];
> char ref1[20];
> char city[30];
> char state[3];
> char zip[10];
> };

>main()
>{
> struct record label[5];
> int count;
> FILE *labelPtr;

> if ((labelPtr = fopen("data.dat", "w")) == NULL)
>   printf("File can not be opened");
> else {
>  for (count = 0; count < 5; count++) {
>   printf("Enter First Name\n");
>   scanf("%s", label[count].fname);
>   printf("Enter Last Name\n");
>   scanf("%s", label[count].lname);
>   printf("Enter Address Line 1\n");
>   scanf("%s", label[count].address1);
>   printf("Enter Adress Line 2\n");
>   scanf("%s", label[count].ref1);
>   printf("Enter City\n");
>   scanf("%s", label[count].city);
>   printf("Enter State (2 character Postal format)\n");
>   scanf("%s", label[count].state);
>   printf("Enter Zip Code (5 digit only please\n");
>   scanf("%s", label[count].zip);

>   fprintf(labelPtr, "%s, ", label[count].fname);
>   fprintf(labelPtr, "%s, ", label[count].lname);
>   fprintf(labelPtr, "%s, ", label[count].address1);
>   fprintf(labelPtr, "%s, ", label[count].ref1);
>   fprintf(labelPtr, "%s, ", label[count].city);
>   fprintf(labelPtr, "%s, ", label[count].state);
>   fprintf(labelPtr, "%s, ", label[count].zip);
>   fprintf(labelPtr, "%c", '\n');
>  }

>  }
>  fclose(labelPtr);
>  return 0;
>}

>--


--



Tue, 09 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
[This followup was also e-mailed to the cited author.]


Quote:
>Hello all,
>I am learning C now and I am confused about something.

>The code that follows works fine with Unix, but when I compile it on a Win95
>machine using Borland I get a fault error and my machine crashes

It would certainly have been nice if you had told us *where* the program
crashes, how far it gets along.

I observe that you are not checking any of the input to see whether it
overflowed the fixed array sizes. That could be a problem.

Try whittling the program down, bit by bit, until it doesn't crash. That
is usually a good way to isolate a problem, since the last thing you
removed is often what was wrong.

--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                                    http://www.mindspring.com/~brahms/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
--



Tue, 09 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing

Quote:
>The code that follows works fine with Unix, but when I compile it on a
Win95
>machine using Borland I get a fault error and my machine crashes.

gp: I recompiled your code under Win95 Borland C and it run fine. No crash.

--



Tue, 09 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing


Quote:
>The code that follows works fine with Unix, but when I compile it on a Win95
>machine using Borland I get a fault error and my machine crashes.

You did remember to compile it as a console application?

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Tue, 09 Oct 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. A newbie needs help with file processing

2. I need HELP with file processing.

3. Newbie needs help reading binary files

4. newbie needs help with opening a data file

5. newbie needs help with disk file

6. newbie needs Borland DOS file help...

7. File Handling - Newbie Help Needed!

8. Newbie needs help using WinInet to copy files (code enclosed)

9. newbie has question on file processing for C.

10. Help C newbie need help please

11. Help C newbie need help please

12. HELP -- NEWBIE NEEDS HELP

 

 
Powered by phpBB® Forum Software