Thread: saving name characters to files

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    saving name characters to files

    Hey guys, I am trying to write a program that will allow a user to write a name save it to a file then recall that name if possible. I also want to use a delete function. Could some one point me in the right direction I have been trying all day and nothing.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Let's see your work so far.

    Todd

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Code:
    #include <stdio.h>
    
    void main()
    {
    FILE *fp;					/*Declare file pointer*/
    int number=4;
    char me[20];
    
    
    
    
    	printf("Enter a name?");
    		scanf("%s", &me);
    
    	
    	fp=fopen("test.txt","w+r");		/*Open file for writing*/
    
    	fprintf(fp,"%d", me);		/*Print number to file*/
    					  
    	fclose(fp);				/*Close file*/
    
    }
    I have done this, I have been taught how to save a intreger to a file. I use this to try to save a name

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    void main is undefined - use int main instead!
    Indent properly. Here's how it should look.
    Don't use scanf to read a string. Read my signature. Read it carefully. I would rather see that scanf gone the next time you post sample code, please.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	FILE *fp;					/*Declare file pointer*/
    	int number=4;
    	char me[20];
    
    	printf("Enter a name?");
    	scanf("&#37;s", &me);
    	
    	fp=fopen("test.txt","w+r");		/*Open file for writing*/
    	fprintf(fp,"%d", me);		/*Print number to file*/
    	fclose(fp);				/*Close file*/
    }
    To save a string to file, you can use fputs. You can read a string with fgets.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    where should I place the fputs?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That should be obvious. Look at file tutorials if you really can't understand how fputs works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving numbers to files
    By cpudaman in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2008, 01:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. saving a double to a file as 4 characters?
    By yahn in forum C++ Programming
    Replies: 5
    Last Post: 12-27-2005, 03:44 PM
  5. Saving large files faster than light!
    By ZapoTex in forum C Programming
    Replies: 19
    Last Post: 01-09-2005, 05:47 PM