Thread: fputs and sequential file

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    fputs and sequential file

    I have been reading up on fgets and fputs and have this current assignment :

    write program that creates a sequential file and writes names and GPAs of several students into following format
    Brown 3.75

    Use fputs to write the whole string into the file. The name and the number should be separated with a single space. Each string should be taken from a keyboard. Repeat reading strings until some prohibited value is entered. Don't forget to close the file in the end of your program.

    The script file for this assignment should contain the text of the program, commands for compiling and execution, and the newly created file with the data.

    END OF INSTRUCTIONS


    OK, I am only asking for explanation as to what this whole assignment means. Safe to say the whole lecture for creation of files and reading and writing to such file went over my head entirely.

    How do I get started on this? Am I writing a source code file that is compiled and run on it's own or am I creating more than one file....like a source code file that when compiled and ran creates an object file???

    Sorry if I sound stupid here. I might just be.
    Give me a bit of room for being slow and stupid. This is my first programming language since 1985 (learned BASIC then).
    I just am not understanding what the logic is of this assignment.
    I have done a program that asks for user input but only by compiling one source code file and running it. Just what is fputs used for??

    note: I checked out this entire site and searched for threads with fputs in it to get an idea, but I am still lost.
    Last edited by sballew; 11-10-2001 at 10:40 PM.
    Sue B.

    dazed and confused


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Without any data validation, this creates a file from keyboard input.
    Code:
    #include <stdio.h>
    
    int main ( ) {
        char buff[BUFSIZ];
        FILE *fp = fopen( "results.txt", "w" );
        while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
            // does buff signal end of input? if so exit the loop
            // does buff match valid input?
            fputs( buff, fp );
        }
        fclose( fp );
        return 0;
    }
    Between the fgets and fputs, you look at buff to decide what the next action should be.
    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.

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Bare with me here....

    from your code

    FILE *fp = fopen( "results.txt", "w" );
    so am I to create a file like results.txt myself for the program (code like you showed me) to read OR is user inputting names and GPAs and results.txt going to be created from that???
    Sue B.

    dazed and confused


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > write program that creates a sequential file
    I figured from this it would be the read from keyboard (stdin) and write to a file kind of program.

    Hence the fgets from stdin, and fputs to a file

    > OR is user inputting names and GPAs and results.txt going to be created from that???
    Yes - that's it exactly
    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.

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok with help of a dear friend, here's the code I have.

    Code:
    #include<stdio.h>
    #define N 100
    
    FILE *fP;
    
    main()
    {
      char str[N];
      int i;
    
      fP = fopen("names.dat", "w");
    
      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++)
      {
       if (*str != 'x')
       {
        printf("Please enter a student's name and their GPA
    separted by a space.\n");
        gets(str);
        if (*str != 'x')
        {
         fputs(str, fP);
         fputs("\n", fP);
         fflush(stdin);
        }
       }
      }
    
      fclose(fP);
    
      return 0;
    }
    Now someone please step me through this. I like to understand in plain English, just what the command lines are doing/asking for.

    And I like to know what the heck a .dat file is. The textbook I have doesn't explain them, it just shows something about input redirecton and gives an example : demo < in.dat The book doesn't explain what this .dat file is. I figure it is the file that my code creates that the data input by user is going to, HOWEVER, for me to grasp this, I want a definition somewhere. Kinda like what is explained about a source code file (eg. file.c ). I understood that in my first semester of C programming, and this semester we got more into that.
    Sue B.

    dazed and confused


  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Mm here goes

    fP = fopen("names.dat", "w");
    This is opening a text file for writing, usually though text files have the extension .txt and binary files have the extension .dat. The "w" is the mode in which the file is opened and this is how the compiler knows whether its a text or binary file, for text we use "wt" or just "w" for binary we use "wb". The mode "w" will create a file called names.dat but beware if you already have a names.dat it will be overwritten!!!!
    Also you should perform some error checking on the success of fopen:
    Code:
    if( (fp = fopen("names.dat", "w")) == NULL){
         printf("%s not opened", "names.dat");
         exit(1);
    }
    The main loop looks a bit iffy, your testing the same expression twice, have another go at it, you might be better placing the test for x in the loop condition. while( i < N && *str != 'x')

    when you use gets it keeps on reading input until the user enters a newline, if the user enters more characters than can be stored in your array other memory will be overwritten, use fgets as it lets you limit the amount of input. fgets(str, strlen(str), stdin);

    oh well thats me typed out. I hope some of that helps. If it wasn't clear(I don't explain things to well) give us a shout.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed