Solved

HOW TO INSERT STRING INTO TOP OF FILE?

Posted on 1998-09-08
4
169 Views
Last Modified: 2010-04-15
HOW TO INSERT STRING INTO TOP OF FILE?(EXAMPLE CODE)
0
Comment
Question by:tanad
4 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 20 total points
ID: 1252552
Due to the workings of most file systems insertions to a file are impossible.
You should create a new file, write the string to it, append the old file to the new, delete the old and rename the new.
0
 
LVL 1

Expert Comment

by:jkrautner
ID: 1252553
Well, I think you have only 2 choices.
1. Write your string in a new file, and then also programmaticaly
   read the old file and write every record into the new file.
or
2. Write your string in a new file, and now spawn a DOS copy
   to append both files together.
   i.e. system("copy string.txt+oldfile.txt new.txt");
 

I think the easiest way is to write your string into a file
and then spawn the copy command to append the old file to
the file
0
 
LVL 3

Expert Comment

by:braveheart
ID: 1252554
It is always helpful to say what operating system you are using.
If you are using UNIX you don't need to use C:

echo "string" > newfile
cat oldfile >> newfile
mv newfile oldfile

This writes the string to the top of a new file and then concatenates the original file to the end of the new file. Finally it renames the new file to have the same name as the original file.
0
 
LVL 8

Expert Comment

by:jhurst
ID: 1252555
actually if you are using dos you can do the same thing as unix, just:
echo "string" >newfile
type oldfile >>newfile
move /y newfile oldfile
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

759 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

19 Experts available now in Live!

Get 1:1 Help Now