473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

change where a file pointer points, inside a function

The following function intends to delete "numberoflines" lines from a text
file, named "s" (string pointer) and pointed to by file pointer "fp", starting
from line "line".

Now, the function works an inteded, but I think that this is purely
coincidential.

I'm using a temp file to copy the contents of the text file, and then, copy
the last lines that i want to keep, on top of those i want to delete.I fwrite
an EOF character after the copied lines, so as to discard the rest of the
file.But this doesn't work!When I open the file with a text editor, I can see
the rest of the lines after a strange character, which evidently represents
EOF.

So, I reopened the temp file (discarding all previous content) and copied the
file again in to it (from start, till EOF, then fclosed it), changed its name
with that of the original file, and then fclose its pointer, and fopened it
using the original files pointer!And it works!The rest of the program uses the
"new" fp...., like it was the originaly fopened one, though its a different
pointer to a different file, only with the same name.

And that leads to the question:when we pass file pointers to functions, isn't
a local copy created?if I fclose a local copy of a file pointer inside a
function, then the "real" file pointer isn't left there dangling? Because from
this program's behavior, it seems like pass by reference is automaticaly used
for file pointers.
int delete_string_f(char* s,FILE* fp,int line,int numberoflines)
{
int check=1;
FILE* fp2=fopen("bak.txt","w+");
rewind(fp);
rewind(fp2);
/*copy the contents of the file pointed by fp to bak.txt*/
filecopy(fp,fp2);

/*move fp to the start of the line we want to delete*/
check=gotoline(line-1,&fp);
if(check==0)
{
printf("EOF encountered\n");
return -1;
}

/*move fp2 to the start of the line after the ones we want to delete*/
check=gotoline(line-1+numberoflines,&fp2);

/*copy the rest of the lines from fp2 on top of the lines we want to delete
from fp*/
if(check!=0)filecopy(fp2,fp);

/*make the file end after the lines we 've copied*/
fprintf(fp,"%c",EOF);

freopen("bak.txt","w+",fp2);
rewind(fp);
/*copying the file from start till the EOF we fprinted in to another file*/
filecopy(fp,fp2);

fclose(fp);
fclose(fp2);
/*remove the original file*/
remove(s);
/*rename the temp file with the original file's name*/
rename("bak.txt",s);
/*change where fp points*/
fp=fopen(s,"r+");

return 0;
}

Nov 14 '05 #1
3 8439
# I'm using a temp file to copy the contents of the text file, and then, copy
# the last lines that i want to keep, on top of those i want to delete.I fwrite
# an EOF character after the copied lines, so as to discard the rest of the
# file.But this doesn't work!When I open the file with a text editor, I can see
# the rest of the lines after a strange character, which evidently represents
# EOF.

Not every file system supports the notion of an end of file character. Unix,
for example, counts the number of bytes and uses that to determine end of file.
Even those that do have might have a different end of file (perhaps (char)26)
than (char)EOF.

In standard C, there's no way to truncate a file, except to truncate to empty
by openning with mode "w" or "w+". Write to the empty file and then close it,
and the system will handle any end of file markers.

Otherwise you'll need to investigate system specific functions, such
as ftruncate.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
We found a loophole; they can't keep us out anymore.
Nov 14 '05 #2
>I'm using a temp file to copy the contents of the text file, and then, copy
the last lines that i want to keep, on top of those i want to delete.I fwrite
an EOF character
EOF isn't a character. And chances are, it won't fit in one
(except on wierd machines where sizeof(int) == 1, which
are legal per ANSI C).
after the copied lines, so as to discard the rest of the
file.
There is no portable way to discard the rest of the file, except
if you're chopping the file to 0 length (fopen(name, "w")), or if
you're copying the file. ftruncate(), chsize(), alakazam() and
truncate() are not portable.
But this doesn't work!When I open the file with a text editor, I can see
the rest of the lines after a strange character, which evidently represents
EOF.
There is no EOF character, but (char) EOF can exist and represent
some character.
So, I reopened the temp file (discarding all previous content) and copied the
file again in to it (from start, till EOF, then fclosed it), changed its name
with that of the original file, and then fclose its pointer, and fopened it
using the original files pointer!And it works!The rest of the program uses the
"new" fp...., like it was the originaly fopened one, though its a different
pointer to a different file, only with the same name. And that leads to the question:when we pass file pointers to functions, isn't
a local copy created?
A local copy of the *POINTER* is created.
A local copy of the FILE structure it points to is not.
if I fclose a local copy of a file pointer inside a
function, then the "real" file pointer isn't left there dangling?
Yes, it's left dangling. The next file you open might by chance
happen to allocate a FILE structure in the same place.
Because from
this program's behavior, it seems like pass by reference is automaticaly used
for file pointers.


Gordon L. Burditt
Nov 14 '05 #3
"Gordon Burditt" <go***********@burditt.org> wrote
A local copy of the *POINTER* is created.
A local copy of the FILE structure it points to is not.
if I fclose a local copy of a file pointer inside a
function, then the "real" file pointer isn't left there dangling?


Yes, it's left dangling. The next file you open might by chance
happen to allocate a FILE structure in the same place.


so it *did* work by chance....And any tampering with the FILE struct would
obviously be system dependant...
Anyway, thanks for the info.

--
Pascal Adoniou

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I had some code that worked fine for several weeks, and then yesterday it stopped working. I'm not sure what I did. Nor can I make out why it isn't working. I'm running a query that should return 3...
5
by: Chathu | last post by:
Hello everyone........... I have a problem on retriving a content of a binary file I wrote into. My program user structures, dynamic allocation of memory and files. I take the infomation into a...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
5
by: Anolethron | last post by:
Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.