Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

HOW TO INSERT STRING INTO TOP OF FILE?

Posted on 1998-09-08
4
196 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

766 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