Thread: file processing help!!!

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28

    file processing help!!!

    For some reason this segment of code does not do what it is supposed to.

    Code:
     float num;
     float balance;
    char filename[25];
    int reg;
                 
    puts("enter registration number");
    scanf(" %d",&reg)
                   
                   sprintf(filename,"%dbal.txt",reg);
                   file = fopen(filename,"r");
                   
                   fscanf(file,"%f",&balance);
                   printf("The current balance for the client is %f\n",balance);
                   fclose(file);
                   
                   puts("enter how much you would like to add to the balance");
                   scanf("%f",&num);
                   
                   file = fopen(filename,"w");
                   balance += num;
                   fprintf(file,"%f",balance);
                   fclose(file);
                   
                   balance = 0;
    it does everything apart from writing balance to the file

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Looks like you're trying to open a file for writing, when it's already open for reading. Since you don't check it for errors (the opening for writing), you don't know WTF.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    Quote Originally Posted by Adak View Post
    Looks like you're trying to open a file for writing, when it's already open for reading. Since you don't check it for errors (the opening for writing), you don't know WTF.
    i closed the file after i opened it for reading. look again.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    for some reason it is not opening the second time

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    plz help

  6. #6
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    i got it to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM