Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need HELP for C code to insert or delete from a text file!

NancyCoderNancyCoder Member Posts: 3
Hi there,

I am very much new in C/C++ programming. I am struggling to write a code in C which will open a text file and delete some content from the given offset until the length of the offset or it will insert the entire content of another file from the offset point.

[color=Blue] sys_insert(const char __user *file, loff_t offset,
const char __user *buf, size_t count,
int flags)[/color]

- is the name(path) of the file you want to insert or delete in
- is the offset where you start the insertion/deletion
- is the user buffer containing the data to be inserted/deleted
- is the length of the data to insert/delete from the offset point
- can be one of these two

#define OPTION_INSERT 1 /* insert data at offset */
#define OPTION_DELETE 2 /* delete data from offset */

The function should return an error number on error (return -1 and set errno), or a positive integer denoting the number of bytes successfully inserted/deleted.

The most important thing functions do first is ensure the validity of the input they are given & check for ALL possible bad conditions that could occur as the result of bad inputs to the function.

If the function was invoked with OPTION_DELETE as the last argument, then the argument must be equal to NULL. Otherwise the input must be considered as invalid.

I just need the C code for the insertion and deletion functions & check the following errors:

- file doesn't exist, not a regular file, no permission
- offset is invalid or past EOF
- buffer is NULL or spans an invalid address space
- count is invalid
- flags are not one of the two allowed flags
- buf is shorter than count
- trying to delete more data than is available in the file (actually, that would NOT be an error, just return however many bytes of data you need).
- ANYTHING else you can think of (the more error checking you do, the better)

The function should return an error number on error (return -1 and set errno), or a positive integer denoting the number of bytes successfully inserted/deleted.

Please help me!

-nancy


So far I could write the code to open the file which is below:

#include
#include

void readLogFile();

int main()

{
readLogFile();
}

void readLogFile()

{
char line[1600];

FILE *fp;
fp=fopen("tj.txt", "r+");
if(fp==NULL)
{

printf("File not found
");

exit(0);

}

else {
printf("File Opened :)
");
while(fgets(line,1600,fp))

{

printf("String: %s", line);

}
}
fclose(fp);
return;
}

Sign In or Register to comment.