i wanna edit one text file using Clanguage.
immedaite........
i have try to use fseek() to move to desired position.
it is also moving, but it is not writing as that position.........
please help me........
the code is as follows..................
// finding location
sf = fopen(s_file, "r");
n = ftell(sf);
//get the pointer position of 103 record to edit it......
while(fscanf(sf,"%d %s",&c1.ac,&c1.name)!=EOF)
{
if(c1.ac==103)
{
printf("%d %s\n",c1.ac,c1.name);
break;
}
else
n = ftell(sf);
}
fclose(sf);
printf("\nre openong file for editing.......");
sf = fopen(s_file, "a+");
fseek(sf,n,0); //move to desired location
fprintf(sf,"%d %s\n",1,"OM"); //writing in desired location
fclose(sf);
Krishnaa
December 6th, 2006, 03:08 AM
If the file is opened in "a+" mode then before write operations the file pointer is moved to the end of the file, so even if you use fseek the next write operation will be carried out after internally setting stream position to file end, so as to avoid overwritting the file contents.
You can not actually edit the file, not like the file editors show on screen, the content insert operation is not supported, so I think you will have to do some kind of trick to get a way around, I would suggest using a temp buffer/file when you can hold the file content from the file position where you want to insert new data upto the end, after insertion is done you can put the temp. contents in the end.
sreehari
December 6th, 2006, 03:15 AM
Are you sure thats the same file that you want to access through the program ? you should also specify the file extension, like s_File.txt, because the a+ mode , if the file is not found will create another file and write to it. so there is a possibility that you are writing into another filr and chceking in another file.
Another point, no matter what, you cannot write into a file at any desired location. All write operations are performed only at the end of the file. so if you want to write something inbetween two existing lines, you minght want to rethink your logic
ujjvalpandya
December 6th, 2006, 03:35 AM
i dnt think so, that C is that much poor that it cant even edit TEXT FILE.....
waht u say>
there msut be some way to directly edit C file.........
i dnt wanna dd line, i just wanna edit the data, of text file.........
wath to do?>
sreehari
December 6th, 2006, 03:43 AM
i dnt think so, that C is that much poor that it cant even edit TEXT FILE.....C is just a language, so calling that poor is not just. How its used in your program, can be termed as poor ;)
i dnt wanna dd line, i just wanna edit the data, of text file.........
wath to do?>
Krishnaa has mentioned in his post a wayabout to implement your requirement.
sreehari
December 6th, 2006, 04:08 AM
Here are a couple of links that discuss how one can Edit files .
static long FileSize(FILE* f) {
/* if function fails, it returns -1 and may have moved the file pointer*/
long ln, sz;
if ((ln=ftell(f))<0 || fseek(f, 0, SEEK_END)<0 || (sz=ftell(f))<0 || fseek(f, ln, SEEK_SET<0))
return -1;
else return sz;
}
static int FileFill(FILE* f,unsigned int length) {
while(length>0) {
if (fputc(' ', f)==EOF) return 0;
--length;
}
return 1;
}
static int MovePositive(FILE* f, unsigned int c_to_insert) {
/* insert c_to_insert space characters in a file at the current file pointer position
the file pointer ftell() is not changed by this function
f must be open in r+b or w+b mode
WARNING: file MUST be open in BINARY mode
*/
unsigned char buffer[FI_BUFFER_SIZE];
long origin=ftell(f), fpos_in_old;
if (origin<0) return 0;
if (fseek(f, 0, SEEK_END)<0
|| (fpos_in_old=ftell(f))<0
|| !FileFill(f, c_to_insert)
|| fseek(f, -c_to_insert, SEEK_CUR)<0) return 0;
while(fpos_in_old>origin) {
size_t size_to_move=(fpos_in_old-origin);
if (size_to_move > FI_BUFFER_SIZE) size_to_move=FI_BUFFER_SIZE;
if (fseek(f, -size_to_move, SEEK_CUR)<0
|| fread(buffer, 1, size_to_move, f)<size_to_move
|| fseek(f, -size_to_move+c_to_insert, SEEK_CUR)<0
|| fwrite(buffer, 1, size_to_move, f)<size_to_move
|| fseek(f, -size_to_move-c_to_insert, SEEK_CUR)<0) return 0;
fpos_in_old-=size_to_move;
}
if (ftell(f)!=origin) return 0; /* sanity check */
FileFill(f, c_to_insert);
return 1;
}
#ifdef TRUNCATE_EXISTS
static int MoveNegative(FILE* f, unsigned int c_to_remove) {
/* remove the c_to_insert bytes preceding the current file pointer position
the file pointer ftell() is not changed by this function
f must be open in r+b or w+b mode
WARNING: file MUST be open in BINARY mode
*/
unsigned char buffer[FI_BUFFER_SIZE];
long origin=ftell(f), old_end;
long fpos_in_old=origin;
if (origin<0 || (old_end=FileSize(f))<0) return 0;
if (c_to_remove > origin) c_to_remove=origin;
#ifdef TRUNCATE_EXISTS
static int xtruncate(const char* filename, unsigned long length) {
int fd,r;
if ((fd=open(filename, O_WRONLY))<0) return 0;
r=ftruncate(fd, length);;
close(fd);
return r>=0;
}
#endif
int Insert(const char* filename, unsigned long pos, int c_to_insert) {
/* insert c_to_insert garbage bytes in a file at the position pos
if c_to_insert is negative, it removes the c_to_insert bytes preceding pos in the file
*/
FILE* f;
if (0==c_to_insert) return 1;
f=fopen(filename, "r+b");
if (!f) return 0;
if (c_to_insert > 0) {
int r;
r=fseek(f, pos, SEEK_SET)>=0 && MovePositive(f, c_to_insert);
fclose(f);
return r;
} else {
unsigned int c_to_remove=-c_to_insert;
#ifdef TRUNCATE_EXISTS
long old_length=FileSize(f);
int r;
if (c_to_remove > pos) c_to_remove=pos;
r=fseek(f, pos, SEEK_SET)>=0 && MoveNegative(f, c_to_remove);
fclose(f);
return r && xtruncate(filename, old_length-c_to_remove);
#else
return FileCopyErase(filename, pos, c_to_remove);
#endif
}
}
static int stringToLong(long* presult, const char* str) {
char* p;
long result;
result=strtol(str, &p, 0);
if (*str=='\0' || *p!='\0' || result==LONG_MAX) return 0;
*presult=result;return 1;
}
int main(int argc, char** argv) {
const char* filename;
long position, c_to_insert;
if (argc!=4) {
fprintf(stderr, "%s expects three arguments: <file name> <position> <number of chars to insert>\n", argv[0]);
return 1;
}
filename=argv[1];
if (!stringToLong(&position, argv[2]) || !stringToLong(&c_to_insert, argv[3])) {
fprintf(stderr, "%s: integer expected!\n", argv[0]);
return 1;
}
return Insert(filename, position, c_to_insert)?0:2;
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.