954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File Handling

hi,

i wanted some help in File handling in C. consider i have one File like a.txt
a.txt

[Marks]

Physics = 60
Chemistry = 80 ;Prac(25)
Maths = 98

[Grade]

Neatness = B
Attention = A ;A+(9.5)

Now suppose i have to insert new values in this file for the subjects or maybe for Grades, then what logic am i suppose to follow?

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 

Do you want to insert one more record to the file or do you want to modify the current values? In any case please mention how you've approached the problem uptil now.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

i want to modify the current values. i feel i should first read the file upto some size into a buffer then in that buffer search for Marks (strstr). after that agn search for Physics in the same way
please correct me if im wrong

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 

You are right. Have you tried to code such functions? Please post them over here using code tags.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

no i haven't and want help in this regard

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 

You are surely not going to get anyone to write the entire code for you. You know how to do the task. Separate it into several small manageable modules. Write the algorithm in a logical order. Refering to each step in the algorithm, write the corresponding C language statements. After you've written the entire code, compile/run it. Try to debug the errors if any. If you are unable to do so, you will definitely get help from this forum. I don't see in what other way I can help you. Please mention if this is not what you were looking for. In that case you need to be more specific with the kind of help you want.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

i am able to extract from file. For eg lets take [Marks] Physics = 60
i am able to extract upto =60, but nw my problem lies that i am not able to replace with new value. how do i do that?

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 

Line 60 is wrong in your code, and lines 88-92 are close....

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

i havn't given any of the code. which code r u talking abt?

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 

on what i know so far, u cant update the contents directly on to the file (well perhaps u can). Perhaps what i would suggest is read each line from the source file into a string. i.e line by line. See if u need to make some changes in that line. IF so do that changes in the string, and write that string back to a new file. And follow the same for all the lines in the source file and keep on copying it on to the new file. Once u have reached EOF on you old file. Delete the old file and rename the new file to the old filename.

That makes it easy for you.

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 
i havn't given any of the code. which code r u talking abt?


Ahh, you got the point -- maybe. But then again, maybe not.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Is there any provision to delete some part of file?

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 
Is there any provision to delete some part of file?


Well u could that through fseek function and try writing few bytes. But u need to be careful through.

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

by trying the way as suggested by u, shall i use fputs to write the string to new file. for reading from the file and putting to string i am using fgets()

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 
by trying the way as suggested by u, shall i use fputs to write the string to new file. for reading from the file and putting to string i am using fgets()

Spot on, thats right, use fgets to read from the old file. Alter the string and write the new string to the new file using fputs or fprintf if u wanted to format the string. This would be more approchable for inital.

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

i thnk fprintf() is a better option

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 
i thnk fprintf() is a better option

It depends up on how u want the write the data on to file. If u think fprintf is good use them.

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

i think i nearly got it. thanks

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 

a new problem ... when i am opening the new file it doesnot have anything. if i take to write only one parameter, like [MARKS] Physics=60
it will be written, but if i try to write multiple things it doesnot support. can you give me some sample code.

carobee
Posting Whiz in Training
209 posts since Dec 2007
Reputation Points: 10
Solved Threads: 12
 
#include <stdio.h>

int main()
{
    FILE *read, *write;
    char Buffer[BUFSIZ];
    
    if( ( read = fopen("Marks.txt", "r") ) ==  NULL )
    {
        printf("Error: File cannot be opened\n");
        return 1;
    }
    
    if( ( write = fopen("TempMarks.txt", "a") ) ==  NULL )
    {
        printf("Error: File cannot be opened\n");
        return 1;
    }    
    
    if( fgets( Buffer, BUFSIZ, read) != NULL )
        fprintf( write, "%s", Buffer );
    
    fprintf( write, "%s - %s\n", "Name", "Name2" );
    
    while( fgets( Buffer, BUFSIZ, read) != NULL )
           fprintf( write, "%s", Buffer );
    
    fclose(read);
    fclose(write);
    
    remove("Marks.txt");
    rename("TempMarks.txt", "Marks.txt");

    return 0;
}


ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You