Solved

basic file functions

Posted on 1998-07-30
4
184 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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