Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify lines in a file

Status
Not open for further replies.

jamescpp

IS-IT--Management
Aug 29, 2001
70
US
Would anyone be willing to provide me with some code to open a file and change some lines? We need to change a registry key on a hundreds of PCs and we're trying to automate it. We can export the key, delete the old key, and install the new key with command lines. Problem is we need to modify it.(eventually in a case statement because some will be different).

I can open a file and close a file. I know how to write to a file using fprintf. What I don't know how to do and haven't figured out, is how do I read lines from a file, and then modify a certain line? Once I could do that, I could just write it all to a new file that could be imported.

For example, the file is:
<start file>
Windows Registry Editor Version 5.00

[HKEY_CURRENT_CONFIG\ImageBuild]
&quot;Modified&quot;=&quot;Release6 December 26, 2001&quot;
<end file>

Needs to be changes to:
<start file>
Windows Registry Editor Version 5.00

[HKEY_CURRENT_CONFIG\ImageBuild]
&quot;Version&quot;=dword:00000006
<end file>

I apologize that I'm not a c programmer. I've barely touched c since my college days. I mostly play around with Java, but I need this in C because our PC's do not have a JRE installed. I've tried finding some stuff on the net, but so far it's all too vague.

Any help is appreciated.

James
 
hello,

To read from a file .
Make sure the file exists,hope you know the codes to do that.
After that , use a fgets method to read a length of strings.the structure is below:

fgets(section,80,readfile)

where section is a character array, 80 is the number of characters you want to read and readfile is the name of the file you are reading from, that is the name you declare when opening yourfile, example

FILE * readfile

print(&quot;Please enter file name: &quot;);
fgets(filename,40,stdin);

readfile = fopen(filename,&quot;r&quot;) // r stands for read mode

if you want to read only floats or integers , you can just use
fscanf(readfile,&quot;%f %f %f&quot;,&x,&y,&z)
the above is an example for a float number

if its a mixture of numbers and strings
use the fgets above and convert by casting any characters into floats or integers

example,

float ddn

ddn = atof(tax) // converts 'tax' character into floats

ddn = atoi(tax) // converts 'tax' character inot integers

if any part is confusing contact on Blacksoarer@hotmail.com
 
As far as modifying lines of a file, in place, you're probably better off creating a new temporary file, writing original lines, including any changes, to that file and then copying the temporary file over the original.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top