Solved

basic file functions

Posted on 1998-07-30
4
180 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
Comment Utility
answers coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
>> 4. errors in finding file.
what do you mean by this?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now