Solved

basic file functions

Posted on 1998-07-30
4
183 Views
Last Modified: 2010-04-02
Hi,
How can I:
1. delete file from a directory (I am not in this directory)?
2. edit a file , also, i am not in this file directory?
3. search a string in a file?
4. errors in finding file.

Thanks A lot,
Tomer Leicht
0
Comment
Question by:tomerl
  • 4
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1169034
answers coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169035
I assume we are talking standard C++ right?  you don't want to be using a specific OS's procedures, like the Windows API?

1. to delete a file use remove() from stdio.h or io.h.  You specify a string that is the path and file name of the file to be removed.

2.  editing a file can mean anything.  (editing a text file is very different than editing a database file)  I assume you mean you want to modify a file in some way.  I'll explain how to modify it in general, you will have to decide ont he specifics for you case.

  you open the file with fopen(), read data from it with fread(), write new data to it with fwrite(), seek to different positions for reading/writting with fseek() and close the file with fclose().  read about these and ask if you have questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169036
3. you open the file and read it in memory then search for the string using strstr().  If the file is too large to read into memory at one time, you can read it in pieces and search each piece.  The danger is if the string starts at the end of one piece and ends at the start of another you won't find it.  There are ways around this.  The easiast (not the most efficeint) is to copy the last (n-1) bytes from the end of the buffer (piece) to the the start where n is the length of the string you are looking for.  Then read in the new piece after those n-1 bytes.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169037
>> 4. errors in finding file.
what do you mean by this?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question