unexpected result, no display of input string

This is a discussion on unexpected result, no display of input string within the C Programming forums, part of the General Programming Boards category; The bolded part doesn't seem to function Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define STRLENGTH 80 int main (int ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Question unexpected result, no display of input string

    The bolded part doesn't seem to function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define STRLENGTH 80
    int main (int argc, char *argv[])
    {
    	FILE *out;
    	char str[STRLENGTH];
    /*	if(argc!=2)
    	{
    		printf("Please enter output location\n");
    		exit(1);
    	}  */
    	if(      (out=fopen("Text","w"))  ==  NULL)
    	{
    		printf("Error in creating file\n");
    		exit(1);
    	}
    	printf("Please enter the text to be inputed:\n");
    	do
    	{
    		gets(str);
    		strcat(str,"\n");
    		fputs(str,out);
    	    if(ferror(out)) printf("Error");
    	}while(*str!='/n');
    
    rewind(out);
    	while(!feof(out))
    	{
    		fgets(str,79,out);
    		printf(str);
    	}	for(;;);
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    You are trying to read from an output stream. Perhaps you should change "w" to "w+" in your call to fopen.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    ... and printf needs a fromat string: printf("%s\n", &str);

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by Perspective
    ... and printf needs a fromat string: printf("%s\n", &str);
    printf(str); is perfectly legal.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    I tried changing "w" to "w+" , it doesn't work.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Edit: Forget everything I just said. Your program is a mess. Here's a fixed version:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define STRLENGTH 80
    int main(int argc, char *argv[])
    {
      FILE *out;
      char str[STRLENGTH];
    
      if((out = fopen("Text", "w+")) == NULL)
      {
        printf("Error in creating file\n");
        exit(1);
      }
      printf("Please enter the text to be inputed:\n");
      do
      {
        fgets(str, STRLENGTH, stdin);
        fputs(str, out);
        if(ferror(out))
          printf("Error");
      } while(*str != '\n');
    
      rewind(out);
      while(fgets(str, STRLENGTH, out))
      {
        if(feof(out))
          break;
        printf(str);
      }
    
      fclose(out);
      return 0;
    }
    Stuff wrong in the original version:
    - You used gets()! Evil! You had a serious buffer overrun vulnerability because of it.
    - You tried reading from an output stream. Changing to mode "w+" fixes that.
    - '/n' isn't even a real character.
    - What's up with the infinite loop toward the end of your program? A more graceful way to not exit immediately is to just use a getchar(); call. Then press ENTER to continue.
    - You didn't call fclose(out);. Open files are closed when the program exits, but it's good form to explicitly close open files.

    Remember: Don't ever ever ever use gets(). Forget it even exists.
    Last edited by itsme86; 08-03-2004 at 08:17 PM.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    - You used gets()! Evil! You had a serious buffer overrun vulnerability because of it.
    - '/n' isn't even a real character.
    - What's up with the infinite loop toward the end of your program? A more graceful way to not exit immediately is to just use a getchar(); call. Then press ENTER to continue.
    Agree with the rest and thanks for those
    Some questions>>
    how do you express CR in string if not '\n'

    what should I use if not gets()

    OH, the loop, although not gentle, saves 0.1 sec typing time per program

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    '\n' is correct, but your original program had '/n' (wrong slash). It might have just been a typo
    Instead of gets() you should use fgets() like I did in the code I pasted.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    thx for the tips, pal

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > printf(str); is perfectly legal.
    Legal perhaps, but massively unwise, error prone and buggy.
    All the user has to do is type in a % character, and you're in a world-of-pain as printf() wanders up the stack looking for non-existant parameters to unknown conversions.

    > fgets(str, STRLENGTH, stdin)
    This should be in a while loop, just like your 2nd use of fgets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by itsme86
    printf(str); is perfectly legal.
    it might compile and "work" but its not standard
    the first string is always the format string with printf, and as Salem pointed out, an input of "%" will cause you a world of pain.
    $man 3 printf
    [...]
    SYNOPSIS
    #include <stdio.h>

    int printf(const char *format, ...);
    int fprintf(FILE *stream, const char *format, ...);
    int sprintf(char *str, const char *format, ...);
    int snprintf(char *str, size_t size, const char *format, ...);

    #include <stdarg.h>

    int vprintf(const char *format, va_list ap);
    int vfprintf(FILE *stream, const char *format, va_list ap);
    int vsprintf(char *str, const char *format, va_list ap);
    int vsnprintf(char *str, size_t size, const char *format, va_list ap);
    [...]
    edit: noting that constant strings are commonly printed as the format string since there isnt any (obvious) danger of a '%' jumping into the buffer.
    Last edited by Perspective; 08-04-2004 at 12:57 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Perspective
    it might compile and "work" but its not standard
    the first string is always the format string with printf, and as Salem pointed out, an input of "%" will cause you a world of pain.


    edit: noting that constant strings are commonly printed as the format string since there isnt any (obvious) danger of a '%' jumping into the buffer.
    Of course it's standard! What on earth do you think a string literal is if not a string? ANY string may be use for the first argument. It's up to you to make your "string" valid, just like every call to every function. It's up to you to make sure you're passing what the function expects you to pass. It most definately is allowed by the standard.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 01-18-2008, 03:14 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21