user1269888
user1269888

Reputation: 47

How do I read and edit a .txt file in C?

I'm writing a program for an ATM. My .txt file is an account balance amount (in this case, 1500.00). How do I read in the .txt file, edit the account balance, and then save it to the file?

For example, if I were to ask a user to enter a deposit an amount of 300.00, I want to be able to add that 300.00 to the existing 1500.00 in the file, and then overwrite the 1500.00 with the total amount of 1800.00.

This is what I have so far.

    float deposit;
    float var;

printf("Current account balance:");
if ( (file_account = fopen ("account.txt", "r")) == NULL)
{
    printf ("Error finding account balance.\n");
    return;
}

while ( (fscanf (file_account, "%c", &var)) != EOF)
{
    printf ("%c", var);
}
printf ("\n");
fclose (file_account);

for (deposit=0; deposit>0; deposit++)
{
    if (deposit > 0)
    {
        printf ("Enter amount to deposit:");
        scanf ("%f", &deposit);
        //file_account + deposit;
        fprintf (file_account, "Your new account balance is: %f", deposit);
    }
    else
    {
        printf ("Amount must be 0 or more.");
    }
    fclose (file_account);

}

Upvotes: 1

Views: 33506

Answers (3)

shailendra
shailendra

Reputation: 271

you can read and edit a file with the help of basic FILE I/O(Input/ output) function in C i an simple manner like for example,writing to a file if it exist and if doesn't create a file with a name and then write to it.

A basic tutorial can be found at http://www.tutorialspoint.com/cprogramming/c_file_io.htm

Now if you are talking about the complex .txt file having a lot of content in it and then you need to find a particular word and change it, i find it a bit difficult rather possible in Linux where you can call a script to read a file , edit it using SED,( stream editor for filtering and transforming text).You can find tutorials for it at below links http://www.panix.com/~elflord/unix/sed.html

Upvotes: 0

Chaos
Chaos

Reputation: 11721

You should use a File pointer to open & read the file, modify the contents according to your will and write back to file.

For example:

File *fp; // creates a pointer to a file
fp = fopen(filename,"r+"); //opens file with read-write permissions
if(fp!=NULL) {
    fscanf(fp,"%d",&var);      //read contents of file
}

fprintf(fp,"%d",var);         //write contents to file
fclose(fp);                   //close the file after you are done

Upvotes: 2

Richard J. Ross III
Richard J. Ross III

Reputation: 55593

You need a few steps here:

int triedCreating = 0;

OPEN:
FILE *filePtr = fopen("test.txt", "r+");

if (!filePtr)
{
    // try to create the file
    if (!triedCreating)
    {
        triedCreating = 1;
        fclose(fopen("test.txt", "w"));
        goto OPEN;
    }
    fprintf(stderr, "Error opening file %i. Message: %s", errno, strerror(errno));
    exit(EXIT_FAILURE);
}

// scan for the float
float value = 0.0f;
fscanf(filePtr, "%f", &value);

printf("current value: %f\nvalue to add: ", value);

// add the new value
float add = 0.0f;
scanf("%f", &add);

value += add;

// write the new value
fseek(filePtr, 0, SEEK_SET);

fprintf(filePtr, "%f", value);

fclose(filePtr);

You may wish to have different formatting for your printf()'s so that it looks nicer when read into a normal text editor.

Upvotes: 1

Related Questions