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 (Compaq Digital), but when I
compile it on a Win95 machine using Borland I get a fault error and my
machine crashes.  Since this 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 (6.0)
at and Win98 at home and have found similar instances (though usually of gcc
returning errors at compile or linux safely closing the program, and more
importantly giving some clue why).

Thank you in advance,
Ben

/*Here's the code. program to input information about mailing addresses and
write to file I know the struck isn't needed for this sequential write, but
I need the pactice */

#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:
}



Sun, 07 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
The only thing that I can tell you is to make sure this is a console mode
application.  If you have created this as a Win32 app, you will have
problems.  Check your docs on creating console apps or check in one of the
Borland newsgroups.

--
Increase the Peace!
Charles LaCour

Quote:

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

>The code that follows works fine with Unix (Compaq Digital), but when I
>compile it on a Win95 machine using Borland I get a fault error and my
>machine crashes.  Since this 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
(6.0)
>at and Win98 at home and have found similar instances (though usually of
gcc
>returning errors at compile or linux safely closing the program, and more
>importantly giving some clue why).

>Thank you in advance,
>Ben

>/*Here's the code. program to input information about mailing addresses and
>write to file I know the struck isn't needed for this sequential write, but
>I need the pactice */

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



Sun, 07 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
Could be the use of scanf().  Read the FAQ about getting user input.  The FAQ is
at:
http://www.eskimo.com/~scs/C-faq/top.html

--
Thomas Matthews



Sun, 07 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
1. You are assuming that each entry will have a certain size, but you are
not checking.
2. You are using scanf(),an evil instrument of mischief, one seen
surprisingly often in student textbooks.

Instead, do it this way:

/* extra declarations required */
#include <string.h>
char *p;

printf("Enter First Name: ");
fflush(stdout);
fgets(label[count].fname,25,stdin);
if((p = strchr(label[count].fname,'\n'))) *p = 0;

For each field, use the actual allocated length in place of the "25" in this
example.

I bet you can think of a way to make this into a subroutine that will make
your code much more robust, efficient, and compact. Yes? Well, here is an
example:

#include <stdio.h>
#include <string.h>

void inputStr(char *prompt,char *buff,int size)
{
 char *p;
 printf("%s: ",prompt);
 fflush(stdout);
 fgets(buff,size,stdin);
 if((p = strchr(buff,'\n'))) *p = 0;

Quote:
}

int main(void)
{
 char buff[128];
 inputStr("Enter a string",buff,128);
 printf("You entered %s\n",buff);
 return 0;
Quote:
}

--

Paul Lutus
www.arachnoid.com

<snip>



Sun, 07 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
Thank you all.  I AM at the Text book level.  Time to start looking outside
of the box.


Quote:
> 1. You are assuming that each entry will have a certain size, but you are
> not checking.
> 2. You are using scanf(),an evil instrument of mischief, one seen
> surprisingly often in student textbooks.

> Instead, do it this way:

> /* extra declarations required */
> #include <string.h>
> char *p;

> printf("Enter First Name: ");
> fflush(stdout);
> fgets(label[count].fname,25,stdin);
> if((p = strchr(label[count].fname,'\n'))) *p = 0;

> For each field, use the actual allocated length in place of the "25" in
this
> example.

> I bet you can think of a way to make this into a subroutine that will make
> your code much more robust, efficient, and compact. Yes? Well, here is an
> example:

> #include <stdio.h>
> #include <string.h>

> void inputStr(char *prompt,char *buff,int size)
> {
>  char *p;
>  printf("%s: ",prompt);
>  fflush(stdout);
>  fgets(buff,size,stdin);
>  if((p = strchr(buff,'\n'))) *p = 0;
> }

> int main(void)
> {
>  char buff[128];
>  inputStr("Enter a string",buff,128);
>  printf("You entered %s\n",buff);
>  return 0;
> }
> --

> Paul Lutus
> www.arachnoid.com


> <snip>



Sun, 07 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing
Given Paul Lutus' code:

Quote:

> int main(void)
> {
>  char buff[128];
>  inputStr("Enter a string",buff,128);
>  printf("You entered %s\n",buff);
>  return 0;
> }

one might suggest (especially when explaning to a 'newbie') a slight
style change to:

#define BUFSIZ 128

int main (void) {
  char buff [BUFSIZ];

  inputStr ("Enter a string", buff, BUFSIZ);
  printf   ("You entered %s\n, buff);

  return 0;

Quote:
}

to reinforce the connection between the size of buff and the third
argument of inputStr().

--
Jonathan Scott Headland
TERMA Elektronik AS              
Bregneroedvej 144                
DK-3460 BIRKEROED        
Denmark



Mon, 08 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing

Quote:

> Given Paul Lutus' code:


> > int main(void)
> > {
> >  char buff[128];
> >  inputStr("Enter a string",buff,128);
> >  printf("You entered %s\n",buff);
> >  return 0;
> > }

> one might suggest (especially when explaning to a 'newbie') a slight
> style change to:

> #define BUFSIZ 128

Since BUFSIZ is defined in <stdio.h>, and is relied on by both <stdio.h>
(<cstdio>) and also <iostream> functions, being defined in terms of <cstdio> by
the standard, this seems like very poor advice.  Yes, I use BUFSIZ as a
replacement for the magic number in Paul's code.  Make sure it is what you
want.  On many systems it is 512.  On mine, it is 16K which may be excessive for
the user's purposes.

--




Mon, 08 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing

Quote:


> > one might suggest (especially when explaning to a 'newbie') a slight
> > style change to:

> > #define BUFSIZ 128

> Since BUFSIZ is defined in <stdio.h>, and is relied on by both <stdio.h>
> (<cstdio>) and also <iostream> functions, being defined in terms of <cstdio> by
> the standard, this seems like very poor advice.  Yes, I use BUFSIZ as a
> replacement for the magic number in Paul's code.  Make sure it is what you
> want.  On many systems it is 512.  On mine, it is 16K which may be excessive for
> the user's purposes.

Yes, sorry, too much thinking like the example code in K&R (which,
of course, uses some names which are already defined in a standard
implementation, such as qsort()), to create a familiar example.
In fact, K&R II uses "BUFSIZE" (p 79).

Not all of the advice was poor, I hope, just the constant's name.

Thanks for the correction.
--
Jonathan Scott Headland
TERMA Elektronik AS              
Bregneroedvej 144                
DK-3460 BIRKEROED        
Denmark



Mon, 08 Oct 2001 03:00:00 GMT  
 A newbie needs help with file processing

Quote:
>Given Paul Lutus' code:


>> int main(void)
>> {
>>  char buff[128];
>>  inputStr("Enter a string",buff,128);
>>  printf("You entered %s\n",buff);
>>  return 0;
>> }

>one might suggest (especially when explaning to a 'newbie') a slight
>style change to:

>#define BUFSIZ 128

BUFSIZ is already defined to be a preprocessor constant in <stdio.h>.
You need to pick a different name here.

--
{ Sunil Rao }
"Is there any light more becoming than that, low and richly tinted, that comes
subdued through the mists of sunshine?"
        -- J Sheridan Le Fanu



Mon, 08 Oct 2001 03:00:00 GMT  
 
 [ 10 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