fopen() fseek() fread() fwrite() 
Author Message
 fopen() fseek() fread() fwrite()


Quote:
>I am trying to put together a little program for the
>benifit of learning the above funtions. I have writtin a
>little program that outputs to a text file called somefile.txt
>. the output looks like this

>                This is a line with blay
>                followed by another line

>                This is a line with blayish
>                followed by another line

>                This is a line with 456
>                followed by another line

>                This is a line with 789
>                followed by another line

>What I wish to do is delete a line eg "This is a line with 456"
>My understanding is that I need to oben somefile.txt, then read up
>to the line containing "This is a line with 456" and write it to a
>tempfile, then read everything after the line and append that
>to the temp file, then delete the text file, then rename the tempfile
>to somefile.txt. So I get this far
>#include <stdio.h>
>#include <string.h>

>char text[80];
>FILE *testfile;
>main()
>{
>puts("What line do you wish to delete? > ");
>fgets(text,sizeof(text),stdin); text[strlen(text)-1] = '\0';

The last statement (text.. = '\0') doesn't do anything, if the string
is already 0-terminated, then strlen - 1 points to the '\0'.
What you propably want to do here is to use strlen - 2 to erase
the '\n' character?

Quote:
>if(strlen(text)>8)
>        {
>        return;

how 'bout an error message before the return?
how 'bout a return value?

Quote:
>        }
>else
>        {
>        testfile = fopen("/home/kevin/somefile.txt","r+");

use the tempfile (or tmpfile maybe, check with man) function
to get a name of temporary file in the /tmp directory. This way
you would not depend on the path '/home/kevin...'.

Quote:
>After this I get a bit lost, I think her I need to use fseek
>somehow to Identify the line, but some have suggested using
>creat for the tempfile and others suggested usinng malloc
>Any help as to what happens next is greatly appreciated.

Use fgets on the file to read one complete line from the file, then use
fputs
to write it into the new file.
To detect the search string (text) in the line, you could use strchr to find
the
occurrences of the first char of the search string, then do a strcmp there.
Or you can download the StringLib from my homepage
http://www.*-*-*.com/ , see software/C,
and use/copy the str_pos function from there.

hth
Ren



Thu, 11 Oct 2001 03:00:00 GMT  
 fopen() fseek() fread() fwrite()

 :>fgets(text,sizeof(text),stdin); text[strlen(text)-1] = '\0';

 : The last statement (text.. = '\0') doesn't do anything, if the string
 : is already 0-terminated, then strlen - 1 points to the '\0'.
 : What you propably want to do here is to use strlen - 2 to erase
 : the '\n' character?

No. foo[strlen(foo)] will point to the '\0' (remember, array indexing
starts at 0), so foo[strlen(foo)-1] will point to the '\n', if there is
one there (you don't know for sure that there is). The comp.lang.c
approved method of getting rid of any trailing newline is:

  {
    char *p = strchr(foo, '\n');
    if (p != NULL) *p = '\0';
  }

--
John Rixon



Thu, 11 Oct 2001 03:00:00 GMT  
 fopen() fseek() fread() fwrite()

Quote:

> I am trying to put together a little program for the
> benifit of learning the above funtions. I have writtin a
> little program that outputs to a text file called somefile.txt


There is very little use for "fseek()", "fwrite()" and "fread()" with text
files. These functions do not take the specific variable line length
characteristic of text files into account. You might use "fread()" and
"fwrite()"  for reading blocks of characters, but you'll have to do an
additional scan for lines of text on the blocks. That is very tedious.

The very simple "fgets()" function (in combination with "fputs()") is best
best thing for reading the lines in a text file.

[snippety snip]

Quote:
> What I wish to do is delete a line eg "This is a line with 456"
> My understanding is that I need to oben somefile.txt, then read up
> to the line containing "This is a line with 456" and write it to a
> tempfile, then read everything after the line and append that
> to the temp file, then delete the text file, then rename the tempfile
> to somefile.txt.

Correct.

Quote:
> #include <stdio.h>
> #include <string.h>

> char text[80];
> FILE *testfile;
> main()

Do not rely on implicite "int". instead make that:
   int main( void )

Quote:
> {
> puts("What line do you wish to delete? > ");
> fgets(text,sizeof(text),stdin); text[strlen(text)-1] = '\0';

Umpf ! Putting separate statement on separate lines is *much* more
readable. And that assignment only works correctly, if there really is a
newline at the end of the string. But if there isn't you will have to deal
with the problem of lines that are too long. In this case the next read
operation will find left-over characters ...

Quote:
> if(strlen(text)>8)
>         {
>         return;

For "int main" this should be something like "return 0;". And you might
want to print a little message saying why the program stops.

Quote:
>         }
> else

Since there is a "return" in the "if" branch, you do not need the "else"
branch. This saves a whole level of indentation.

Quote:
>         {
>         testfile = fopen("/home/kevin/somefile.txt","r+");

> After this I get a bit lost,

Ehem, you get lost quite early. First of all for the input file you need
only read access mode, ie. "r" is sufficient, "r+" is unnecessary and
might allow accidential write access.

Quote:
> I think her I need to use fseek
> somehow to Identify the line,

No, I am afraid that is not possible. That's well beyond what "fseek()"
can do. In fact, as I explained earlier, "fseek()" is of limited use to
text files. Read it's description in your compiler manual or C book to
find out more about it.

What you have to do is read the file line by line (use "fgets()" in a
loop) and check each line against your identification condition (whatever
that is). All lines that do not match get written to the destination file
(some temporary file opened with "w" mode, use the "fputs()" function for
writing). A line that matches simply does not get written to the
destination file. When you have reached the end of the input file you can
close both files.

Now you can use the "remove()" and "rename()" functions to finish your
operation.

Quote:
> but some have suggested using
> creat for the tempfile

"fopen()" will be quite sufficient, if you can come up with your own file
name. If you can't you can use the "tmpnam()" function to get one.

Quote:
> and others suggested usinng malloc

Hmmm, I'd say that a simple array for reading one line of the file will be
OK. Use something like:
   char line[250];

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Fri, 12 Oct 2001 03:00:00 GMT  
 fopen() fseek() fread() fwrite()
Rene Eng schrieb:

Quote:

> The last statement (text.. = '\0') doesn't do anything, if the string
> is already 0-terminated, then strlen - 1 points to the '\0'.
> What you propably want to do here is to use strlen - 2 to erase
> the '\n' character?

No, he was correct and you are off by one. For a string terminated with a
'\n' and a '\0' the expression "text[strlen(text)-1]" will give you the
'\n' character.

Quote:
> use the tempfile (or tmpfile maybe, check with man) function
> to get a name of temporary file in the /tmp directory. This way
> you would not depend on the path '/home/kevin...'.

The path he gave aparently represented the input file, not the temporary
file. Note also that the "tmpfile()" function is defined as opening a
binary file, not a text file. This might be a problem. The "tmpnam()"
function in combination with "fopen()" is better.

Quote:
> To detect the search string (text) in the line, you could use strchr to find
> the
> occurrences of the first char of the search string, then do a strcmp there.

Or you might use the function "strstr()" right away.

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Fri, 12 Oct 2001 03:00:00 GMT  
 fopen() fseek() fread() fwrite()


<snip>
Quote:
> My most concerning hitch
> would seem to be how to read the null pointer returned by fgets()
> when it reaches the end of a file, other additions I have added may
> vary from mildly to wildly inaccurate. Once again, many thanks for all
> your help  I have the code below

<snip>

Up at the top of the function, char *FgetsPtr;

Quote:
> while(num==1)
>         {

        FgetsPtr = fgets(list,sizeof(list),testfile);
        if(FgetsPtr == NULL)

--
Richard Heathfield

The bug stops here.



Mon, 15 Oct 2001 03:00:00 GMT  
 fopen() fseek() fread() fwrite()

Quote:



> I have learned a good deal from your response and based on that
> I have added some appropriate alterations, however, thi of course
> brings new and uncharted areas for me. My most concerning hitch
> would seem to be how to read the null pointer returned by fgets()
> when it reaches the end of a file, other additions I have added may
> vary from mildly to wildly inaccurate. Once again, many thanks for all
> your help  I have the code below

 while (fgets(buff, size, stream)) { /* process */ }
or, equivalently,
 while (fgets(buff, size, stream) != 0) { /* process */ }

You don't need to "read" the null pointer returned by fgets().  If you want to
store it, you can use
 char *fgets_flag;
 fgets_flag = fgets(buff, size, stream);
 if (fgets_flag) { /* do something */ }

The essential point is that the null pointer is something to test for, not to
use.

--




Mon, 15 Oct 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. fseek,fread,fseek,fwrite

2. fseek fread fwrite fflush

3. fread()/fseek()/fwrite() problem?

4. fopen, fwrite, fread, fclose

5. using fwrite and fseek!

6. extra space created by fwrite after fseek past end of file

7. NEWBIE problem with fseek, and fwrite

8. fseek() and fread()

9. FSEEK and FREAD

10. ASCII file read vs. fseek()/fread()

11. fread, fwrite probs

12. Please Help (fread, fwrite)

 

 
Powered by phpBB® Forum Software