Newbie - fopen help needed 
Author Message
 Newbie - fopen help needed

To all:
I'm writing a small C program (DOS) to open a text file.  Using fopen,
the prog works if I put the file name to open in double quotes (i.e.
fileptr = fopen ("file.txt", "r");
I can also work it with a command line argument.  But, I want to use a
variable  so I can open different files.  I'm getting lost with my
string variable and satisfying fopen to need a pointer.  Can someone
'point' me in the right direction (no pun intended :)?
Thanks in advance,

--



Wed, 21 Sep 2005 15:23:52 GMT  
 Newbie - fopen help needed

Quote:
> To all:
> I'm writing a small C program (DOS) to open a text file.  Using fopen,
> the prog works if I put the file name to open in double quotes (i.e.
> fileptr = fopen ("file.txt", "r");
> I can also work it with a command line argument.  But, I want to use a
> variable  so I can open different files.  I'm getting lost with my
> string variable and satisfying fopen to need a pointer.  Can someone
> 'point' me in the right direction (no pun intended :)?
> Thanks in advance,


Assuming you want to get the file name interactively you can do something
like this

#include <stdio.h>
#include <stdlib.h>

static void die(char *);

int main(void)
{
    int rv;
    FILE *myfile;
    char filename[257];

    printf("Enter a file to open: ");
    fflush(stdout);

    rv = scanf("%256[^\n]", filename);
    if (rv == EOF)
        die("End of file reached...");
    else if (rv < 1)
        die("I/O error detected...");
    else
    {
        /* Everything is OK, clean up extraneous input */
        scanf("%*[^\n]");
        getchar();
    }

    myfile = fopen(filename, "r");
    if (myfile == NULL)
    {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    /* ... */

    return 0;

Quote:
}

static void die(char *msg)
{
    fprintf(stderr, "Fatal error: %s\n", msg);
    exit(EXIT_FAILURE);
Quote:
}

--



Sat, 24 Sep 2005 07:30:16 GMT  
 Newbie - fopen help needed

wrote in comp.lang.c.moderated:

Quote:

> > To all:
> > I'm writing a small C program (DOS) to open a text file.  Using fopen,
> > the prog works if I put the file name to open in double quotes (i.e.
> > fileptr = fopen ("file.txt", "r");
> > I can also work it with a command line argument.  But, I want to use a
> > variable  so I can open different files.  I'm getting lost with my
> > string variable and satisfying fopen to need a pointer.  Can someone
> > 'point' me in the right direction (no pun intended :)?
> > Thanks in advance,

> Assuming you want to get the file name interactively you can do something
> like this

> #include <stdio.h>
> #include <stdlib.h>

  #include <string.h>

Quote:

> static void die(char *);

Prefer:

static void die(const char *);

Quote:
> int main(void)
> {
>     int rv;
>     FILE *myfile;
>     char filename[257];

FILENAME_MAX would be a better choice than 257, and it is defined in
<stdio.h>.

Quote:
>     printf("Enter a file to open: ");
>     fflush(stdout);

>     rv = scanf("%256[^\n]", filename);

Oh, good heavens!

Quote:
>     if (rv == EOF)
>         die("End of file reached...");
>     else if (rv < 1)
>         die("I/O error detected...");

   if (NULL != fgets(filename, sizeof filename, stdin))
   {
      char *nlp = strchr(filename, '\n');
      if (nlp)
      {
         *nlp = '\0';
      }
      else
      {
         die("name too long!");
      }

Quote:
>     else
>     {
>         /* Everything is OK, clean up extraneous input */
>         scanf("%*[^\n]");

No way of detecting string too long?

Quote:
>         getchar();
>     }

>     myfile = fopen(filename, "r");
>     if (myfile == NULL)
>     {
>         perror("Error opening file");

fopen() is not required to set errno on failure, so this is not
universally useful.

Quote:
>         exit(EXIT_FAILURE);
>     }

>     /* ... */

>     return 0;
> }

> static void die(char *msg)

static void die(const char *msg)

Quote:
> {
>     fprintf(stderr, "Fatal error: %s\n", msg);
>     exit(EXIT_FAILURE);
> }

Good constant hygiene is important for healthy programs!

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Sun, 25 Sep 2005 10:47:07 GMT  
 Newbie - fopen help needed
Thank you for your help!  In addition to your suggestion, I found that
if I manually added a null term to the filename string, then the fopen
would process it properly.  Funny thing is that I thought strcpy was
supposed to automatically do that...I couldn't get that to work.
Thanks for your help,



Quote:

>> To all:
>> I'm writing a small C program (DOS) to open a text file.  Using fopen,
>> the prog works if I put the file name to open in double quotes (i.e.
>> fileptr = fopen ("file.txt", "r");
>> I can also work it with a command line argument.  But, I want to use a
>> variable  so I can open different files.  I'm getting lost with my
>> string variable and satisfying fopen to need a pointer.  Can someone
>> 'point' me in the right direction (no pun intended :)?
>> Thanks in advance,

>Assuming you want to get the file name interactively you can do something
>like this

>#include <stdio.h>
>#include <stdlib.h>

>static void die(char *);

>int main(void)
>{
>    int rv;
>    FILE *myfile;
>    char filename[257];

>    printf("Enter a file to open: ");
>    fflush(stdout);

>    rv = scanf("%256[^\n]", filename);
>    if (rv == EOF)
>        die("End of file reached...");
>    else if (rv < 1)
>        die("I/O error detected...");
>    else
>    {
>        /* Everything is OK, clean up extraneous input */
>        scanf("%*[^\n]");
>        getchar();
>    }

>    myfile = fopen(filename, "r");
>    if (myfile == NULL)
>    {
>        perror("Error opening file");
>        exit(EXIT_FAILURE);
>    }

>    /* ... */

>    return 0;
>}

>static void die(char *msg)
>{
>    fprintf(stderr, "Fatal error: %s\n", msg);
>    exit(EXIT_FAILURE);
>}
>--


--



Sun, 25 Sep 2005 10:47:24 GMT  
 Newbie - fopen help needed
Quote:

> Thank you for your help!  In addition to your suggestion, I found that
> if I manually added a null term to the filename string, then the fopen
> would process it properly.  Funny thing is that I thought strcpy was
> supposed to automatically do that...I couldn't get that to work.

[...]

strcpy() copies up to and including the nul that's supposed to already
be there.  If the source wasn't nul-terminated, then strcpy() will
keep copying until it gets one or crashes the program.

How were you using strcpy() ?

--

+---------+----------------------------------+-----------------------------+
| Kenneth |     kenbrody at spamcop.net      | "The opinions expressed     |
|    J.   |    http://www.hvcomputer.com     |  herein are not necessarily |
|  Brody  |      http://www.fptech.com       |  those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
--



Mon, 26 Sep 2005 06:50:40 GMT  
 Newbie - fopen help needed
Greetings,
Instead of doing
newstringvar = stringvar + '\0';

I thought that

strcpy(newstringvar, stringvar);

would do the same thing.

Thanks,



Quote:

>> Thank you for your help!  In addition to your suggestion, I found that
>> if I manually added a null term to the filename string, then the fopen
>> would process it properly.  Funny thing is that I thought strcpy was
>> supposed to automatically do that...I couldn't get that to work.
>[...]

>strcpy() copies up to and including the nul that's supposed to already
>be there.  If the source wasn't nul-terminated, then strcpy() will
>keep copying until it gets one or crashes the program.

>How were you using strcpy() ?

>--

>+---------+----------------------------------+-----------------------------+
>| Kenneth |     kenbrody at spamcop.net      | "The opinions expressed     |
>|    J.   |    http://www.hvcomputer.com     |  herein are not necessarily |
>|  Brody  |      http://www.fptech.com       |  those of fP Technologies." |
>+---------+----------------------------------+-----------------------------+
>--


--



Wed, 28 Sep 2005 06:23:31 GMT  
 Newbie - fopen help needed

Quote:

> Instead of doing
> newstringvar = stringvar + '\0';
> I thought that
> strcpy(newstringvar, stringvar);
> would do the same thing.

Ah, in that case you have a fundamental misperception.
Not every array of type char is considered a "string" in C.
There is a convention that a C string *must* be terminated
by a zero-valued element, usually called a null terminator.
The str*() library functions, among others, follow this
convention.  So strcpy() expects its *input* to already be
null terminated, and it will copy the null terminator also,
to sensure that its target is null terminated.  Some
functions (such as the whole mem*() family) handle arrays
of type char without any assumption about their content,
and those functions do not know about null termination.
You have to use whatever is appropriate given your inputs
and desired outputs.  Often, drawing diagrams (showing the
terminators!) helps to get this right.
--



Fri, 30 Sep 2005 12:25:12 GMT  
 Newbie - fopen help needed
Thank you for teaching me this aspect of C.  Great example of using
these forums.  Thanks again.



Quote:

>> Instead of doing
>> newstringvar = stringvar + '\0';
>> I thought that
>> strcpy(newstringvar, stringvar);
>> would do the same thing.

>Ah, in that case you have a fundamental misperception.
>Not every array of type char is considered a "string" in C.
>There is a convention that a C string *must* be terminated
>by a zero-valued element, usually called a null terminator.
>The str*() library functions, among others, follow this
>convention.  So strcpy() expects its *input* to already be
>null terminated, and it will copy the null terminator also,
>to sensure that its target is null terminated.  Some
>functions (such as the whole mem*() family) handle arrays
>of type char without any assumption about their content,
>and those functions do not know about null termination.
>You have to use whatever is appropriate given your inputs
>and desired outputs.  Often, drawing diagrams (showing the
>terminators!) helps to get this right.
>--


--



Sun, 02 Oct 2005 08:26:09 GMT  
 Newbie - fopen help needed
*** top posting fixed ***

Quote:



> >> Instead of doing
> >> newstringvar = stringvar + '\0';
> >> I thought that
> >> strcpy(newstringvar, stringvar);
> >> would do the same thing.

> >Ah, in that case you have a fundamental misperception.
> >Not every array of type char is considered a "string" in C.
> >There is a convention that a C string *must* be terminated
> >by a zero-valued element, usually called a null terminator.
> >The str*() library functions, among others, follow this
> >convention.  So strcpy() expects its *input* to already be
> >null terminated, and it will copy the null terminator also,
> >to sensure that its target is null terminated.  Some
> >functions (such as the whole mem*() family) handle arrays
> >of type char without any assumption about their content,
> >and those functions do not know about null termination.
> >You have to use whatever is appropriate given your inputs
> >and desired outputs.  Often, drawing diagrams (showing the
> >terminators!) helps to get this right.

> Thank you for teaching me this aspect of C.  Great example of
> using these forums.  Thanks again.

And your reply was a poor example.  Please DO NOT toppost.  Put
your reply after the material to which you are referring, and snip
the useless portions.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Mon, 03 Oct 2005 14:52:57 GMT  
 Newbie - fopen help needed

Quote:


>>Instead of doing
>>newstringvar = stringvar + '\0';
>>I thought that
>>strcpy(newstringvar, stringvar);
>>would do the same thing.

> Ah, in that case you have a fundamental misperception.
> Not every array of type char is considered a "string" in C.
> There is a convention that a C string *must* be terminated
> by a zero-valued element, usually called a null terminator.
> The str*() library functions, among others, follow this
> convention.  So strcpy() expects its *input* to already be
> null terminated, and it will copy the null terminator also,
> to sensure that its target is null terminated.  Some
> functions (such as the whole mem*() family) handle arrays
> of type char without any assumption about their content,
> and those functions do not know about null termination.
> You have to use whatever is appropriate given your inputs
> and desired outputs.  Often, drawing diagrams (showing the
> terminators!) helps to get this right.

I find it usefull, esp. in debugging, to always clear out an array
first;  for example:

    memset( stringvar, '\0', sizeof(stringvar));
--



Sat, 15 Oct 2005 23:45:00 GMT  
 Newbie - fopen help needed
On 29 Apr 2003 15:45:00 GMT in comp.lang.c.moderated, bagsmode

Quote:



>>>Instead of doing
>>>newstringvar = stringvar + '\0';
>>>I thought that
>>>strcpy(newstringvar, stringvar);
>>>would do the same thing.

>> Ah, in that case you have a fundamental misperception.
>> Not every array of type char is considered a "string" in C.
>> There is a convention that a C string *must* be terminated
>> by a zero-valued element, usually called a null terminator.
>> The str*() library functions, among others, follow this
>> convention.  So strcpy() expects its *input* to already be
>> null terminated, and it will copy the null terminator also,
>> to sensure that its target is null terminated.  Some
>> functions (such as the whole mem*() family) handle arrays
>> of type char without any assumption about their content,
>> and those functions do not know about null termination.
>> You have to use whatever is appropriate given your inputs
>> and desired outputs.  Often, drawing diagrams (showing the
>> terminators!) helps to get this right.

>I find it usefull, esp. in debugging, to always clear out an array
>first;  for example:

>    memset( stringvar, '\0', sizeof(stringvar));

*stringvar = '\0'; does all that is required, and works even if
stringvar is a pointer, whereas your code only works if stringvar
is defined as an array.

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

    fake address                use address above to reply
--



Tue, 18 Oct 2005 15:27:22 GMT  
 Newbie - fopen help needed
Thanks for all your input!

On 02 May 2003 07:27:22 GMT, Brian Inglis

Quote:

>On 29 Apr 2003 15:45:00 GMT in comp.lang.c.moderated, bagsmode



>>>>Instead of doing
>>>>newstringvar = stringvar + '\0';
>>>>I thought that
>>>>strcpy(newstringvar, stringvar);
>>>>would do the same thing.

>>> Ah, in that case you have a fundamental misperception.
>>> Not every array of type char is considered a "string" in C.
>>> There is a convention that a C string *must* be terminated
>>> by a zero-valued element, usually called a null terminator.
>>> The str*() library functions, among others, follow this
>>> convention.  So strcpy() expects its *input* to already be
>>> null terminated, and it will copy the null terminator also,
>>> to sensure that its target is null terminated.  Some
>>> functions (such as the whole mem*() family) handle arrays
>>> of type char without any assumption about their content,
>>> and those functions do not know about null termination.
>>> You have to use whatever is appropriate given your inputs
>>> and desired outputs.  Often, drawing diagrams (showing the
>>> terminators!) helps to get this right.

>>I find it usefull, esp. in debugging, to always clear out an array
>>first;  for example:

>>    memset( stringvar, '\0', sizeof(stringvar));

>*stringvar = '\0'; does all that is required, and works even if
>stringvar is a pointer, whereas your code only works if stringvar
>is defined as an array.

>Thanks. Take care, Brian Inglis     Calgary, Alberta, Canada
>--

>    fake address            use address above to reply
>--


--



Thu, 27 Oct 2005 05:54:16 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. newbie needs help with fopen & stdin

2. Need help with fopen() in Borland C++ 4.5

3. fopen problem when allocating big matrices : need help

4. Need help with fopen/fread prob in W98 - PLEASE

5. Need help with fopen, file ptr problem

6. Help C newbie need help please

7. Help C newbie need help please

8. HELP -- NEWBIE NEEDS HELP

9. newbie fopen() question.

10. Newbie question: Need help please!

11. NEWBIE in need of help

12. A newbie needs help with file processing

 

 
Powered by phpBB® Forum Software