?
Solved

HOW TO INSERT STRING INTO TOP OF FILE?

Posted on 1998-09-08
4
Medium Priority
?
215 Views
Last Modified: 2010-04-15
HOW TO INSERT STRING INTO TOP OF FILE?(EXAMPLE CODE)
0
Comment
Question by:tanad
[X]
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
4 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 60 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

771 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