error in the output file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaunms
    New Member
    • May 2007
    • 4

    error in the output file

    Hello Friends,

    This is my first post, I am new to programming, trying to learn unix c programming , i needed a help, I have a small program to write a simple data to a file which is created through fopen. the code is below :
    -------------------------------------------------
    #include<stdio. h>
    #include<stdlib .h>
    #include<string .h>
    int main()
    {
    FILE *john;
    char typo[1000];

    john=fopen("/home/cpp/john.txt", "wt");
    if(!john)
    {
    perror("Failed opening file john.txt for appending\n");
    }

    printf("enter your name\n");
    fscanf(stdin, "%s", &typo);

    fwrite(&typo, sizeof(typo) , 10, john);

    fclose(john);

    return 0;

    }
    -----------------------------------------------------------------

    The program is getting compiled properly without any errors, even i run the executable file, it asks for my name and then i enter "my name is shaun" after that when i check the john.txt file, it shows all junk characters filled in the file, can anyone help me out with this.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    First, learn to use CODE tags. They are part of the forum rules, so even if you feel like ignoring etiquette, don't ignore the rules.

    Next: fscanf(stdin, "%s", &typo);

    scanf requires pointers to variables. Remember that arrays are a special case. When you refer to an array in a function, syntactically, that's like referring to a pointer.

    That is, you should have written:

    Code:
    fscanf(stdin, "%s", typo);
    I recommend reading the C-FAQ on this subject.

    Comment

    • shaunms
      New Member
      • May 2007
      • 4

      #3
      Hello,

      Thank you for the reply. I am sorry, i didn't mean to break the rules, This is my first time, i will take care, the next time i post. But Sir, I am still not clear with the fscanf thing you mentioned, I checked the C-FAQ link you had provided, but didn't get any examples of fscanf. Can you help a little more. I tried the :

      [code=c]
      fscanf(stdin, "%s", typo);
      [/code]

      But its still the same. the output file john.txt is filled with junk characters.

      Thank you
      Shaun

      Comment

      • primeSo
        New Member
        • Aug 2007
        • 35

        #4
        Originally posted by shaunms
        Hello Friends,

        This is my first post, I am new to programming, trying to learn unix c programming , i needed a help, I have a small program to write a simple data to a file which is created through fopen. the code is below :
        -------------------------------------------------
        #include<stdio. h>
        #include<stdlib .h>
        #include<string .h>
        int main()
        {
        FILE *john;
        char typo[1000];

        john=fopen("/home/cpp/john.txt", "wt");
        if(!john)
        {
        perror("Failed opening file john.txt for appending\n");
        }

        printf("enter your name\n");
        fscanf(stdin, "%s", &typo);

        fwrite(&typo, sizeof(typo) , 10, john);

        fclose(john);

        return 0;

        }
        -----------------------------------------------------------------

        The program is getting compiled properly without any errors, even i run the executable file, it asks for my name and then i enter "my name is shaun" after that when i check the john.txt file, it shows all junk characters filled in the file, can anyone help me out with this.
        First of all, you need to understand the fundamental of each library function provided by C.

        As you mention in your question, you want to write a string into your text file.

        You use "wt" as a accessing mode, is it correct? shoudn't it be "w" or "a" only? please check it out.

        fscanf(arg1, arg2, arg3) take in 3 arguments, the first one is the FILE type pointer, the 2nd is format string, and the last is the memory location. to store the input. Do you understand in and out regarding this function? It read in input from data file only. It is reasonable you use STDIN in this case. You don't need a "&" for an array. Please check out the reason.

        You simply dump a fwrite() function and hope it will works as you expected? i am sure you will need more reading on this. sizeof(typo) is used wrongly, this will only return the size of a character on your machine. check it out the reason too. To check the length of a string, u must use string library function i.e. strlen(typo).

        My suggestion to you, use printf() instead if fwrite() to write data into a text file. check it out the usage of both of them and you will know which one is easier for u to understand for the momeny.

        Extra information, C is a type loose language and it allows programmer to do a lot of things that seems correct, in fact it is wrong when you run them. Thus, you have to learn and master each function in order to achieve what you want with your program.

        Comment

        • shaunms
          New Member
          • May 2007
          • 4

          #5
          Hello,

          Thanks for the reply buddy, now there is no junk character in my output file. I have modified the code:

          [code=c]

          #include<stdio. h>
          #include<stdlib .h>
          #include<string .h>
          int main()
          {
          FILE *sohail;
          char typo[1000];

          sohail=fopen("/home/suhail.thakur/cpp/sohail.txt", "w");
          if(!sohail)
          {
          perror("Failed opening file sohail.txt for writing\n");
          }

          printf("enter your name\n");
          fscanf(stdin, "%s", typo);

          fprintf(sohail, "%s", typo);

          fclose(sohail);

          return 0;

          }

          [/code]

          I used the fprintf function.
          I accept what you have said, we need to master each and every function.
          I have just started learning a programming language and this is my first programming language I am trying to learn, was stuck on this thing, so just wanted some help.
          I am still not complete, at present, the new problem I am facing is. when the program executes, and when i type "my name is shaun" it just writes the first word "my" in the output file. Well i'll try to find out some more, Thanks for the help friends.


          Thank you
          Shaun

          Comment

          • shaunms
            New Member
            • May 2007
            • 4

            #6
            Hello friends,

            I made some further changes and my code is working perfectly as i wanted:

            [code=c]

            #include<stdio. h>
            #include<stdlib .h>
            #include<string .h>
            int main()
            {
            FILE *john;
            char typo[1000];

            john=fopen("/home/cpp/john.txt", "wt");
            if(!john)
            {
            perror("Failed opening file john.txt for appending\n");
            }

            printf("enter your full name\n");

            fgets(typo, 500, stdin);

            fputs(typo, john);

            fclose(john);

            return 0;

            }

            [/code]

            Once again Thanks for the help buddies. Just pasting my little code, If anybody would find it helpful.


            Thank you
            Shaun

            Comment

            • primeSo
              New Member
              • Aug 2007
              • 35

              #7
              Originally posted by shaunms
              Hello,

              Thanks for the reply buddy, now there is no junk character in my output file. I have modified the code:

              [code=c]

              #include<stdio. h>
              #include<stdlib .h>
              #include<string .h>
              int main()
              {
              FILE *sohail;
              char typo[1000];

              sohail=fopen("/home/suhail.thakur/cpp/sohail.txt", "w");
              if(!sohail)
              {
              perror("Failed opening file sohail.txt for writing\n");
              }

              printf("enter your name\n");
              fscanf(stdin, "%s", typo);

              fprintf(sohail, "%s", typo);

              fclose(sohail);

              return 0;

              }

              [/code]

              I used the fprintf function.
              I accept what you have said, we need to master each and every function.
              I have just started learning a programming language and this is my first programming language I am trying to learn, was stuck on this thing, so just wanted some help.
              I am still not complete, at present, the new problem I am facing is. when the program executes, and when i type "my name is shaun" it just writes the first word "my" in the output file. Well i'll try to find out some more, Thanks for the help friends.


              Thank you
              Shaun
              You are more welcome ^.^

              For your knowledge wise, scanf() and fsanf() will get every input from your buffer untill it hit the first white space character (tab, new line, space etc), thus, if your enter an string which consists white space as your mentioned, fscang() only accepts the first word ie "my" only.. there is two ways to resolve this problm

              1. use "%[^\n]" in your format string argument i.e. 2nd argumnet. It means read in everything except new line. Check the "^" operator, namely "circumflex ".

              2. what u have done is correct... use fgets() ^.^

              Good luck and be well & happy~

              Comment

              Working...