Thread: file handling

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    Smile file handling

    Our lecturer gave us a program to look over in the holidays. Unfortunately I think he has forgotten we haven’t done any file handling in C yet. I’ve had a look around on some other websites to see if I can self teach myself some stuff. In my opinion I think from what I have picked up this piece of code should work? I’m trying to add a name to a txt file that already has some data in it …

    From the examples I read online they were only printing text already present in the source code into the text file. But I am assuming that it’s possible to add the content of a variable to a text file?

    When I run the program I enter a name and then press enter and the program closes. When I check the text file nothing has changed at all. Please help

    Chris

    Code:
    void add_student()
    {
         char student_name[25];
          printf("\nPlease enter a students name to add them to the data base.");
          scanf("%24s",&student_name);
    
                                    FILE *fp;
                                    fp = fopen("collegerecord.txt","a");
                                    fprintf(fp,"s%",student_name);
                                    fclose(fp);
     
         //puts("Function associated with menu option 'b' called sucessfully");
         //getch();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets works to read a line of text into a buffer. From there you could use sscanf on it. You could use fscanf which works like scanf. You could use fgetc which works like getc.

    Basically you can scan into a buffer and parse it, or you can read directly into a variable. You just decide which method you need, and pick the right functions for the task.

    As to your issue, try removing the & from your scanf line. Also try displaying the name on the screen first to make sure it's how you think it is.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

Tags for this Thread