'Exit' character in sequential file. I'm sure it is simple...

This is a discussion on 'Exit' character in sequential file. I'm sure it is simple... within the C Programming forums, part of the General Programming Boards category; Could someone point out why my exit character 'x' is being written to my sequential file? I would really appreciate ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    'Exit' character in sequential file. I'm sure it is simple...

    Could someone point out why my exit character 'x' is being written to my sequential file? I would really appreciate it. Thank You!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    
    FILE *fP;
    
    main()
    {
        char str[N];
        int i;
    
        fP = fopen("names.dat", "w");
    
        if ((fP = fopen("names.dat", "w")) == NULL)
            {
                printf("%s not opened", "names.dat");
                exit(EXIT_FAILURE);
            }
    
    
        printf("This program will write names and GPAs into a file named names\n");
        printf("Please enter an x to exit the program.\n");
    
        for(i=0; i<N; i++)
            {
                while (i < N && *str != 'x')
                    {
                        printf("Please enter a student's name and their GPA separted by a space.\n");
                        gets(str);
                        fputs(str, fP);
                        fputs("\n", fP);
                        fflush(stdin);
                    }
            }
    
        fclose(fP);
    
        return 0;
    }
    Also if you see any improvements I can make, please feel free to point it out. With that being said, please be kind.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
                        gets(str);
                        fputs(str, fP);
    That's why. You read a string, then immediately write it, then hit the loop check. There are so many errors in this program, I hardly know where to start. Well I suppose I'll just start at the top...

    1. Avoid globals. Technically not "wrong", just not good.
    2. main should be returning an int. Read the FAQ.
    3. Why are you calling fopen twice?
    4. You don't need to check i<N twice.
    5. You don't dereference array names. I'm surprised this compiles.
    6. gets. Read the FAQ.
    7. fflush(stdin). Read the FAQ.


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
                        gets(str);
                        fputs(str, fP);
    That'll do it every time.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by quzah View Post
    Code:
                        gets(str);
                        fputs(str, fP);
    That's why. You read a string, then immediately write it, then hit the loop check. There are so many errors in this program, I hardly know where to start. Well I suppose I'll just start at the top...

    1. Avoid globals. Technically not "wrong", just not good.
    2. main should be returning an int. Read the FAQ.
    3. Why are you calling fopen twice?
    4. You don't need to check i<N twice.
    5. You don't dereference array names. I'm surprised this compiles.
    6. gets. Read the FAQ.
    7. fflush(stdin). Read the FAQ.


    Quzah.
    Sweet! Thanks!

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by quzah View Post
    Code:
                        gets(str);
                        fputs(str, fP);
    That's why. You read a string, then immediately write it, then hit the loop check. There are so many errors in this program, I hardly know where to start. Well I suppose I'll just start at the top...

    1. Avoid globals. Technically not "wrong", just not good.
    2. main should be returning an int. Read the FAQ.
    3. Why are you calling fopen twice?
    4. You don't need to check i<N twice.
    5. You don't dereference array names. I'm surprised this compiles.
    6. gets. Read the FAQ.
    7. fflush(stdin). Read the FAQ.


    Quzah.
    Forgive me, but I am not understanding the "dereference of array names." Could you help me understand it? Thanks!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by csharp100 View Post
    Forgive me, but I am not understanding the "dereference of array names." Could you help me understand it? Thanks!
    This:
    Code:
    char str[N];  <--- array
    ...
                while (i < N && *str != 'x') <-- dereferencing
    You only dereference actual pointers, not arrays.

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

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by quzah View Post
    This:
    Code:
    char str[N];  <--- array
    ...
                while (i < N && *str != 'x') <-- dereferencing
    You only dereference actual pointers, not arrays.

    Quzah.
    Thanks for the info! I appreciate it.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Still having problems here. I have to use a do/while loop. I put the fputs outside the loop and still get an "x" character inside my file. Can anyone help me with this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 100
    
    int main()
    {
        FILE *fP;
        char str[N], *p;
        p = str;
        int counter = 0;
    
        if ((fP = fopen("names.dat", "w")) == NULL)
        {
            printf("%s not opened", "names.dat");
            exit(EXIT_FAILURE);
        }
    
        printf("This program will write names and GPAs into a file named names\n");
        printf("Please enter an x to exit the program.\n");
    
            do
            {
                gets(str);
                fputs(str, fP);
                fputs("\n", fP);
    
            }while (*p != 'x');
    
    
    
            fclose(fP);
    
        return 0;
    }
    Also is the pointer used correctly for the array?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
                gets(str);
                fputs(str, fP);
    Do you see anything at all between these lines that would be able to prevent you from doing the second one (printing to the file) after the first one has happened? Once you read it in, you spit it directly out.

    Also why bother with p? And for that matter what if the file contains "xenon"? Are you supposed to quit on that x as well, or just an x by itself?

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by tabstop View Post
    Code:
                gets(str);
                fputs(str, fP);
    Do you see anything at all between these lines that would be able to prevent you from doing the second one (printing to the file) after the first one has happened? Once you read it in, you spit it directly out.

    Also why bother with p? And for that matter what if the file contains "xenon"? Are you supposed to quit on that x as well, or just an x by itself?
    As I had said before, I have tried putting the fputs OUTSIDE of the do/while loop and still get an "x" character inside my file. I assume that the character "x" is inside single quotes in the "does not equall" condition is not the same as xenon inside single quotes. Is that not correct?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by csharp100 View Post
    As I had said before, I have tried putting the fputs OUTSIDE of the do/while loop and still get an "x" character inside my file.
    When you get outside of the loop, "x" is all you have so of course that's what gets printed. What you need is read / check / if-ok-then-print, not read / print / check / print.
    Quote Originally Posted by csharp100 View Post
    I assume that the character "x" is inside single quotes in the "does not equall" condition is not the same as xenon inside single quotes. Is that not correct?
    That is amazingly not correct. *p represents just the first character of the string, not the string-as-a-whole.

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by tabstop View Post
    When you get outside of the loop, "x" is all you have so of course that's what gets printed. What you need is read / check / if-ok-then-print, not read / print / check / print.

    That is amazingly not correct. *p represents just the first character of the string, not the string-as-a-whole.
    Fair enough, than what do you suggest for an exit character?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem isn't necessarily the character itself. It's how you check for it. You should be doing something like this:
    Code:
    while true
        read a line
        if line matches exit
            break;
        else
            write line
    If you want to check for a single character against another single character, you can. If you want to compare strings, you need something like strcmp.

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

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Guys, I understand what you are saying but I am not fathoming it inside a do/while loop in which it must be done this way. If it was not for that directive, I would not be on here. It is the logic of the do/while I am getting lost in. From my instructor, "repeat reading strings until some prohibited value is entered (use do/while loop). I do not see how I can read a string, check that string then print it if it is correct when I am the one creating the file. It is not reading from another file. This program is only worth 7 points so it cannot be a bunch of lines of code. That is the way she operates.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    while (1)
       {        
          gets(str);
          if (strcmp(str,"x") != 0)    
            { fputs(str, fP);
               fputs("\n", fP); }
          else
            break;
       }
    Like they've been telling you to do...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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