help, simple program crashes... 
Author Message
 help, simple program crashes...

Can someone help me?  I just learned how to use FILE and fopen, fclose..
so I decided to make a project for myself, which was to mimic what
cat does under UNIX.  cat is a program that takes in characters from
either STDIN or a file, then spits it out to STDOUT.

Can someone tell me why my code works fine under Linux and not under
MSDOS/WIN95.  I'm using the same compiler on both platforms, GCC and
DJGPP.  Is my code wrong?  It looks fairly simple and standard...

Thanks for any help anyone can offer.

Vinh

=======================================================================

/*      Program:        cat
        Author:         Vinh Luu
        Description:

        My implementation of cat for unix.      */

#include<stdio.h>

int main(int argc, char *argv[])
{
        FILE *infile;
        char c;
        int i=2;

        if ( argc > 1 )
        {
                while ( i <= argc )
                {
                        infile = fopen(argv[i],"r");
                        while (fscanf(infile,"%c",&c) != EOF)
                        {
                                printf("%c",c);
                        }
                        fclose(infile);
                        i++;
                }
        }
        else
        {
                while (scanf("%c",&c) != EOF)
                {                
                        printf("%c",c);
                }
        }

        return 0;

Quote:
}



Thu, 13 Apr 2000 02:00:00 GMT  
 help, simple program crashes...

Quote:

>         int i=2;

>         if ( argc > 1 )
>         {
>                 while ( i <= argc )
>                 {
>                         infile = fopen(argv[i],"r");

argv[1] is the first command line input, yet you start with i = 2.
When i == argc, argv[i] is a NULL pointer, so fopen() returns
NULL. You then try to scan from that in the next statement.

You should ALWAYS check the return value!

if(infile == NULL)
{
    /* Do something else */

Quote:
}

else
{
     /* Use infile */
Quote:
>                         while (fscanf(infile,"%c",&c) != EOF)



Thu, 13 Apr 2000 02:00:00 GMT  
 help, simple program crashes...

Quote:

> Can someone help me?  I just learned how to use FILE and fopen, fclose..
> so I decided to make a project for myself, which was to mimic what
> cat does under UNIX.  cat is a program that takes in characters from
> either STDIN or a file, then spits it out to STDOUT.

> Can someone tell me why my code works fine under Linux and not under
> MSDOS/WIN95.  I'm using the same compiler on both platforms, GCC and
> DJGPP.  Is my code wrong?  It looks fairly simple and standard...

> Thanks for any help anyone can offer.

> Vinh

> =======================================================================

> /*      Program:        cat
>         Author:         Vinh Luu
>         Description:

>         My implementation of cat for unix.      */

> #include<stdio.h>

> int main(int argc, char *argv[])
> {
>         FILE *infile;
>         char c;
>         int i=2;

 ***** Why 2 instead of 1?

Quote:

>         if ( argc > 1 )

***** argc is top or cap value (one index unit past last string.

***** If argc = 2, then command line has program name plus one argument
***** and that argument will be argv[1]
***** not argv[2]
***** This works just the same for UNIX except that in UNIX you can't depend on argv[0] being process (program) path and file name

Quote:
>         {
>                 while ( i <= argc )

***** Never happens with only one argument on the command line

Quote:
>                 {
>                         infile = fopen(argv[i],"r");
>                         while (fscanf(infile,"%c",&c) != EOF)

***** Better to avoid scanf for stuff like this
***** fgetc or getchar and fputc or putchar would be better

***** The pros would never use either however, because both are too slow
***** Instead they would use fread and fwrite to read and write large blocks at the same time

Quote:
>                         {
>                                 printf("%c",c);
>                         }
>                         fclose(infile);
>                         i++;
>                 }
>         }
>         else
>         {
>                 while (scanf("%c",&c) != EOF)

***** I never use scanf.  Does this format mean "Get input from stdin?"

Quote:
>                 {
>                         printf("%c",c);
>                 }
>         }

>         return 0;

***** Aha!  And that is precisely what this program is doing, isn't it?

Quote:
> }

Cheers!

Chaumont Devin.



Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...



        [snip]

Quote:
> You should also check your file for appropriate syntax. If the
user types
> in c:\mydir\myfile you will want to double up on the

backslashes and make

Quote:
> it c:\\mydir\\myfile

The above is not necessary, and will cause errors, on MS-DOS and
Windows.  The only time the '\' needs to be doubled is when
writing string constants in C source code, because the compiler
strips the '\' looking for valid escape sequences.  This is not
necessary for strings received from the command line, read from
another file, etc.

Jack



Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...


Quote:
>You should also check your file for appropriate syntax. If the user types
>in c:\mydir\myfile you will want to double up on the backslashes and make
>it c:\\mydir\\myfile

Does one really want this?
I have to use double backslashes in my source if only one is needed (and
even then not always), but I assume that opening "c:\\mydir\\myfile" will
fail, where opening "c:\mydir\myfile" might work.

        +------------------------+             "Life is just a situation
        |   Jan van den Broek    |              Life is just a game
        +------------------------+              Life is just a whirlpool

        +------------------------+                  - The Residents, Amber



Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...


Quote:
> Can someone help me?  I just learned how to use FILE and fopen, fclose..
> so I decided to make a project for myself, which was to mimic what
> cat does under UNIX.  cat is a program that takes in characters from
> either STDIN or a file, then spits it out to STDOUT.

> Can someone tell me why my code works fine under Linux and not under
> MSDOS/WIN95.  I'm using the same compiler on both platforms, GCC and
> DJGPP.  Is my code wrong?  It looks fairly simple and standard...

> Thanks for any help anyone can offer.

> Vinh

> =======================================================================

> /*      Program:        cat
>         Author:         Vinh Luu
>         Description:

>         My implementation of cat for unix.      */

> #include<stdio.h>

> int main(int argc, char *argv[])
> {
>         FILE *infile;
>         char c;
>         int i=2;

>         if ( argc > 1 )
>         {
>                 while ( i <= argc )
>                 {
>                         infile = fopen(argv[i],"r");
>                         while (fscanf(infile,"%c",&c) != EOF)
>                         {
>                                 printf("%c",c);
>                         }
>                         fclose(infile);
>                         i++;
>                 }
>         }
>         else
>         {
>                 while (scanf("%c",&c) != EOF)
>                 {                
>                         printf("%c",c);
>                 }
>         }

>         return 0;
> }

I have no idea about Linux, but under DOS argc returns the total number of
command line elements with the first being the path and program name. These
are stored in argv[0] through argv[(argc-1)]. I don't know what the
standard says, but I assume that anything in argv[argc] and beyond are
undefined and uninitialized.

So start with i=1 and check for (i<argc).

Also, always check your file pointer after opening a file to make sure it
isn't equal to NULL - especially if you're writing to it.

You should also check your file for appropriate syntax. If the user types
in c:\mydir\myfile you will want to double up on the backslashes and make
it c:\\mydir\\myfile

Along these same lines, although you don't have any of these situations in
this code snippet, before writing to a string or other array, be sure that
it is impossible for your code to write beyond the end of the declared
array - real easy to do with some of the string functions unless you check
their lengths before hand. Also make sure that every string you manipulate
is null terminated when you get done with it - either as a required result
of the function you're using or because you explicitly null terminate it.  



Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...



Quote:


>    [snip]

> > You should also check your file for appropriate syntax. If the
> user types
> > in c:\mydir\myfile you will want to double up on the
> backslashes and make
> > it c:\\mydir\\myfile

> The above is not necessary, and will cause errors, on MS-DOS and
> Windows.  The only time the '\' needs to be doubled is when
> writing string constants in C source code, because the compiler
> strips the '\' looking for valid escape sequences.  This is not
> necessary for strings received from the command line, read from
> another file, etc.

> Jack

Unless some fool programs this:

int main (int argc, char *argv[])
{
        char s[100];

        fgets (s, 100, stdin);
        printf (s);     /* Instead of printf ("%s", s);
        return 0;

Quote:
}

Marco.
---------------------
Swearing is the only language spoken proficiently by programmers.


Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...


Quote:


> >You should also check your file for appropriate syntax. If the user types
> >in c:\mydir\myfile you will want to double up on the backslashes and make
> >it c:\\mydir\\myfile

> Does one really want this?
> I have to use double backslashes in my source if only one is needed (and
> even then not always), but I assume that opening "c:\\mydir\\myfile" will
> fail, where opening "c:\mydir\myfile" might work.

The standard denotes that you should use forward slashes _not_ backslashes
no matter what your OS says.  The problem is that the backslash is used
to indicate special characters \n, \t etc and so \newfile will be read
as <newline>ewfile.

The solution is simple, use forward slashes instead.

--
|Un Loup en France  | Wolverhampton Wanderers, out of darkness cometh Bully|
|---------------- C++ is to OO what C is to structured --------------------|
|----The above opinions rarely reflect my own and never my employers'------|
|Do not add me to mailing lists violations will be billed for time.        |



Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...



Quote:




>>       [snip]

>> > You should also check your file for appropriate syntax. If the
>> user types
>> > in c:\mydir\myfile you will want to double up on the
>> backslashes and make
>> > it c:\\mydir\\myfile

>> The above is not necessary, and will cause errors, on MS-DOS and
>> Windows.  The only time the '\' needs to be doubled is when
>> writing string constants in C source code, because the compiler
>> strips the '\' looking for valid escape sequences.  This is not
>> necessary for strings received from the command line, read from
>> another file, etc.

>> Jack

>Unless some fool programs this:

>int main (int argc, char *argv[])
>{
>        char s[100];

>        fgets (s, 100, stdin);
>        printf (s);     /* Instead of printf ("%s", s);
>        return 0;
>}

The only special character in a printf format string is %. \ is *not*
special in this context. As stated above \ is special in string literals
(and also in character constants). It happens that printf format strings
are usually specified as string literals so here \ is special because it is
in a string literal, not because it is in a printf format string.

It might help to remember that \ is special when it appears in source code
but it is not special when it appears as data in the execution environment,
and for example "\n\\" in the source code is translated to a string
consisting of a single new-line character and a single \ character in the
execution environment (followed by the usual null character).
strlen("\n\\") yields 2.

--
-----------------------------------------


-----------------------------------------



Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...

I'll have to check this out more carefully. I was pretty sure that I've
written programs where I had to type in double back slashes from the
keyboard to get it to access files correctly. Maybe I'm thinking of code
where I created a format string and sent that to a function? Maybe it's
another artifact of a poor compiler (early TurboC compiler)? But I know
that I had to write a function to search an input string and add in a
second backslash before using it to access a file.



Quote:


>    [snip]

> > You should also check your file for appropriate syntax. If the
> user types
> > in c:\mydir\myfile you will want to double up on the
> backslashes and make
> > it c:\\mydir\\myfile

> The above is not necessary, and will cause errors, on MS-DOS and
> Windows.  The only time the '\' needs to be doubled is when
> writing string constants in C source code, because the compiler
> strips the '\' looking for valid escape sequences.  This is not
> necessary for strings received from the command line, read from
> another file, etc.

> Jack



Fri, 14 Apr 2000 03:00:00 GMT  
 help, simple program crashes...



Quote:


> The only special character in a printf format string is %. \ is *not*
> special in this context. As stated above \ is special in string literals
> (and also in character constants). It happens that printf format strings
> are usually specified as string literals so here \ is special because it
is
> in a string literal, not because it is in a printf format string.

> It might help to remember that \ is special when it appears in source
code
> but it is not special when it appears as data in the execution
environment,
> and for example "\n\\" in the source code is translated to a string
> consisting of a single new-line character and a single \ character in the
> execution environment (followed by the usual null character).
> strlen("\n\\") yields 2.

> --
> -----------------------------------------


> -----------------------------------------

Sorry, you're right, I'm wrong. Big excuses for giving wrong
information. mea culpa, etc..

Marco.
---------------------
Swearing is the only language spoken proficiently by programmers.



Sat, 15 Apr 2000 03:00:00 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Help please, simple test program crashes

2. Simple MFC program crashes in NTDLL.DLL

3. Program crashes(Help)

4. Help !, Crashing File reading program

5. Program (68k Mac) crashes upon exitting (need help beyond FAQ)

6. Help, program crashes

7. HELP with debugging a crashed program

8. HELP: program crash when deleting string in CCheckListBox

9. HELP with debugging a crashed program

10. Help on program crash

11. (Help!): Program crashes if user cancels internet connection

12. Help- my program is crashing

 

 
Powered by phpBB® Forum Software