Thread: Writing to a file

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    26

    Writing to a file

    Hiya everyone, i have a problem ive been trying to write to a file but when i open that file its empty. The programs builds so ive come to the conclusion that im missin something or i have written it wrong.

    Code:
    int Registration(int* user)
    {
    	FILE *db;
    	
    	char chr;
    	
    	int putc(int ch, FILE *db);
    	
    		db = fopen("Info.txt", "a");
    		
    		printf ("Enter Registration user here:");
    	
    		chr = getchar();
    		
    		scanf  ("c", &chr);
    		
    		putc(chr, db);
    	
    		printf ("\nRegistration Accepted!!");
    	
    		
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Close the file. Every fopen should have a corresponding fclose.
    My best code is written with the delete key.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int putc(int ch, FILE *db);
    Function declarations should be at the start of your source file or in your header. And since putc is a C runtime function, you should include the appropriate header rather than redeclare the function prototype.

    Code:
    		chr = getchar();
    		scanf  ("c", &chr);
    Essentially, you're reading TWO character and overwriting the value of the previous value you read. What's the sense in that?

    You also never close the function and you never use the user argument.
    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. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM