Need help with fopen() in Borland C++ 4.5 
Author Message
 Need help with fopen() in Borland C++ 4.5

Hi;
I try to open file "record.dat" and copy to structure. Although the file
"record.dat" resides on the disk, I still getting message "File could
not be opened". My disk is not write-protected and I tried several
attempts to alter the text line "a:\record.dat", "rt" (e.g.,
"a:\\record.dat", "rt" or "\\record.dat", "rt") This is a partial code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_RECORDS 13
#define MAX_FIELDS   6
#define FALSE 0
#define TRUE  1

 struct address
 {
  char name[20];
  char street[20];
  char town[15];
  char state[3];
  char zipcode[6];
  char phone[13];
 };

main ()
{
 struct address personal[MAX_RECORDS];
 char string[81];

 FILE *in_file;
 int u, i;

 char *token_ptr;
 char smColon[] = { ";"};

 if((in_file = fopen("a:\\record.dat", "rt")) == NULL)
 {
  printf("File could not be opened\n");
 }
 else
 {

  for(u=0; u <= MAX_RECORDS - 1; u++)
  {
   while(fgets(string, 81, in_file) != NULL )
   {
    do
    {

     for(i = 0; i <= MAX_FIELDS - 1; i++)
     {
      token_ptr = strtok(string, smColon);
     switch(i)
     {
     case 0:
      token_ptr[sizeof(personal->name) - 1] = '\0';
      strcpy(personal->name, token_ptr);
      break;
     case 1:
      token_ptr[sizeof(personal->street) - 1] = '\0';
      strcpy(personal->street, token_ptr);
      break;
     case 2:
      token_ptr[sizeof(personal->town) - 1] = '\0';
      strcpy(personal->town, token_ptr);
      break;
     case 3:
      token_ptr[sizeof(personal->state) - 1] = '\0';
      strcpy(personal->state, token_ptr);
      break;
     case 4:
      token_ptr[sizeof(personal->zipcode) - 1] = '\0';
      strcpy(personal->zipcode, token_ptr);
      break;
     case 5:
      token_ptr[sizeof(personal->phone) - 1] = '\0';
      strcpy(personal->phone, token_ptr);
      break;
     }
     }
    }while(token_ptr = strtok(NULL, smColon));

   }

  }

 fclose(in_file);



Sat, 26 May 2001 03:00:00 GMT  
 Need help with fopen() in Borland C++ 4.5

   I try to open file "record.dat" and copy to structure. Although the file
   "record.dat" resides on the disk, I still getting message "File could
   not be opened". My disk is not write-protected and I tried several
   attempts to alter the text line "a:\record.dat", "rt" (e.g.,
   "a:\\record.dat", "rt" or "\\record.dat", "rt") This is a partial code:

A few suggestions:

1. Try printing the error message, it may be helpful:

        perror ("fopen");

2. There is no "rt" file mode.  Use "r".
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Sat, 26 May 2001 03:00:00 GMT  
 Need help with fopen() in Borland C++ 4.5

Quote:

> 2. There is no "rt" file mode.  Use "r".

I want to make this more clear for the possibly confused OP (biennui).
The only modes which are standardly defined in C are

        Text            Binary
        "r"           "rb"  open file for reading
        "w"           "wb"  open file for writing
                                truncate to zero length or create
        "a"           "ab"  open file for writing at end of file
                                open or create if not present
        "r+"          "rb+" open file for update
                        "r+b"
        "w+"          "wb+" truncate or create, open for update
                        "w+b"
        "a+"          "ab+" open for update at end of file
                        "a+b"

Other characters which follow these modes, as in "rt" are not defined in
the standard.  On at least one system this is used to open a tape file
for reading.  It is likely that your implementation has defined "rt" as
being the same as "r" in standard C.  The reason for this is that may
have used another non-standard feature, a global variable named _fmode
which makes "r" act as "rb" if _fmode==O_BINARY.  This is non-standard,
illegal, and not portable.  Stick to the above and forget about _fmode.

--




Sun, 27 May 2001 03:00:00 GMT  
 Need help with fopen() in Borland C++ 4.5

Quote:

> Hi;
> I try to open file "record.dat" and copy to structure. Although the file
> "record.dat" resides on the disk, I still getting message "File could
> not be opened". My disk is not write-protected and I tried several
> attempts to alter the text line "a:\record.dat", "rt" (e.g.,
> "a:\\record.dat", "rt" or "\\record.dat", "rt") This is a partial code:

[snippety snip]

Hi biennui,

For a basic file opening test let's reduce your code to the essential
part:

  #include <stdio.h>

  int main( void )
  {
    FILE *in_file;

    in_file = fopen("a:\\record.dat", "r" );
    if( in_file != NULL)
    {
       printf( "Heureka: the file has been opened\n" );
       fclose( in_file );
    }
    else
      printf("Error: File could not be opened\n");

    return 0;
  }

This little test program should work in normal circumstances. If the
file really exists and can not be opened with this test program,
the problem most certainly is of an external nature, ie. not caused
by the program and most likely not by the compiler either.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 27 May 2001 03:00:00 GMT  
 Need help with fopen() in Borland C++ 4.5


Quote:

> > 2. There is no "rt" file mode.  Use "r".

> I want to make this more clear for the possibly confused OP (biennui).
> The only modes which are standardly defined in C are

>    Text            Binary
>    "r"           "rb"  open file for reading
>    "w"           "wb"  open file for writing
>                            truncate to zero length or create
>    "a"           "ab"  open file for writing at end of file
>                            open or create if not present
>    "r+"          "rb+" open file for update
>                    "r+b"
>    "w+"          "wb+" truncate or create, open for update
>                    "w+b"
>    "a+"          "ab+" open for update at end of file
>                    "a+b"

> Other characters which follow these modes, as in "rt" are not defined in
> the standard.  On at least one system this is used to open a tape file
> for reading.  It is likely that your implementation has defined "rt" as
> being the same as "r" in standard C.  The reason for this is that may
> have used another non-standard feature, a global variable named _fmode
> which makes "r" act as "rb" if _fmode==O_BINARY.  This is non-standard,
> illegal, and not portable.  Stick to the above and forget about _fmode.

<Jack>

You're explanation of the history behind "rt" and "wt" in Borland and
Microsoft compilers is right on the mark, but your final statement is a little
too strong...

<Quibble Mode>

Quote:
> This is non-standard, illegal, and not portable.

It is non-standard and not portable, but _NOT_ illegal.  The standard requires
that the string start with one of a specifically enumerated set of characters,
of which 'r' is one, but specifically allows additional characters as
implementation-defined extensions.

</Quibble Mode>

</Jack>



Mon, 28 May 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. NEED HELP Borland c 4.5

2. Help Borland C++ version 4.5 / Builder 1.0 for Windows 95

3. DOS program creation in Borland C++ 4.5 help!

4. ## Help with Borland C++ 4.5 and BDE! ###

5. Borland C++ 4.5 vs Visual C++

6. NYC C++ meetings: Borland C++ 4.5; Multi-threading

7. NEW need help with turbo C++ 4.5

8. Getting input in Borland C++ 4.5

9. Borland C++ 4.5 (using the delay() command)

10. Borland C++ 4.5

11. Creating DLL for Vb 6.0 with Borland C++ 4.5

12. Question about Borland C++ 4.5 compliler

 

 
Powered by phpBB® Forum Software