Thread: Help writing to file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Help writing to file

    Hello I wrote this program it's meant to take the final value of the soconday voltage or the secondary current and send the final value to a file saved in the the c: drive called test. The program excutes well but when i go back to see if the values are in the file it's not there. I was hoping Someone could fix my code and point out the mistakes. I would be highly obliged is someone takes 5 mins of thir time to do so.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	float voltageP,voltageS,currentP,currentS,N1,N2,i;
    	FILE *inp, *outp;
    	
    printf(" If you want to calculate in terms of voltage enter 1 for current enter any other number: ");
    scanf("%f",&i);
    
    if (i==1) {
    	printf("What is the primary voltage: ");
    	scanf("%f",&voltageP);
    	printf("How many turns in the primary coil: ");
    	scanf("%f",&N1);
    	printf("How many turns in the secondary coil: ");
    	scanf("%f",&N2);
    	voltageS=voltageP*N2/N1;
    	printf("The secondary voltage is equal to %f volts\n",voltageS);
    	inp =fopen("c:test.txt", "w");
    }
    
    else {
    	printf("What is the primary current: ");
    	scanf("%f",&currentP);
    	printf("How many turns in the primary coil: ");
    	scanf("%f",&N1);
    	printf("How many turns in the secondary coil: ");
    	scanf("%f",&N2);
    	currentS=currentP*N2/N1;
    	printf("The secondary current is equal to %f amps\n",currentS);
    	inp =fopen("c:test.txt", "w");
    }
    return 0;
    }

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Always check the file pointers to NULL.
    I guess you are waiting for almighty to write the values to the file .because I cant see any code you have written to write the values to files.Do some search on board on how to write on files incase you dont know.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    In your code, you have to properly open the file. Please pay attention to the double backslashes in the fopen statement. Also, you have to write data to the file using fprintf etc. And finally you have to close the file.

    Bob

    Code:
    	inp =fopen("c:\\test.txt", "w");
    	 fprintf( inp, "%f %f %f %f\n", voltageP, voltageS, N1, N2 );
    	 fclose(inp);

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And finally you have to close the file.
    Well, you don't have to. But it's a good idea to do so.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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