Insert in a file without overwriting without using a 2nd file using C/C++
The below are the contents in a text file.
name1: 1234
name2: 2000
name3: 3000
This is an existing text file and I want to replace one value(say 1234) with another value (say 12345) in the text file. so I placed the cursor at start of the value (here its the 7th position) . Then i used the following statement:
fprintf(filepointer,"12345\n");
The resultant file is like
name1: 12345
ame2 : 2000
name3 : 3000
Its overwriting the 4 characters("1000") and a newline('\n') and 'n' with 5 characters("12345") and a newline('\n').
The solutions I know are:
1. Overwriting the entire file to add one extra character.
2. Copying each line in a linked list node and change the characters in the memory and write in the same file.
3. Create a temp file and copy the new characters to the desired place in the temp file and change the name of the temp file to source file name and delete the source file.
Also I tried adding carriage return '\r' and windows format of EOF ('\r\n') , still the next line characters are overwritten. Also I expanded the file size using [SetEndOfFile][1] API and still I face the same problem. I searched many forums and found answers like "It is not possible to insert characters without overwriting".
Is there any solution just to insert characters without overwriting the characters in the middle of the file. or any logic to insert characters in a line and not affect the next line.
Thanks in advance.
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Is there any solution just to insert characters without overwriting the characters in the middle of the file. or any logic to insert characters in a line and not affect the next line.
No, while you can overwrite characters anywhere in the file, you can only add characters to the end of the file.
Re: Insert in a file without overwriting without using a 2nd file using C/C++
To insert additional or remove existing character(s) which are not at the end of the file then either solution 2) or 3) would be used depending upon the size of the file.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Originally Posted by GiriPrakash
The below are the contents in a text file.
name1: 1234
name2: 2000
name3: 3000
This is an existing text file and I want to replace one value(say 1234) with another value (say 12345) in the text file. so I placed the cursor at start of the value (here its the 7th position) . Then i used the following statement:
fprintf(filepointer,"12345\n");
The resultant file is like
name1: 12345
ame2 : 2000
name3 : 3000
Its overwriting the 4 characters("1000") and a newline('\n') and 'n' with 5 characters("12345") and a newline('\n').
The solutions I know are:
1. Overwriting the entire file to add one extra character.
2. Copying each line in a linked list node and change the characters in the memory and write in the same file.
3. Create a temp file and copy the new characters to the desired place in the temp file and change the name of the temp file to source file name and delete the source file.
Also I tried adding carriage return '\r' and windows format of EOF ('\r\n') , still the next line characters are overwritten. Also I expanded the file size using [SetEndOfFile][1] API and still I face the same problem. I searched many forums and found answers like "It is not possible to insert characters without overwriting".
Is there any solution just to insert characters without overwriting the characters in the middle of the file. or any logic to insert characters in a line and not affect the next line.
Thanks in advance.
Update:
Is it possible Using visual studio VC++ ??
Thanks
Last edited by ovidiucucu; May 31st, 2016 at 11:01 AM.
Reason: Added [/QUOTE] tag
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Originally Posted by GiriPrakash
Is there any solution just to insert characters without overwriting the characters in the middle of the file. or any logic to insert characters in a line and not affect the next line.
There couldn't be any logic like that. Just could not. Because a file is always a solid stream of bytes, and it knows nothing about characters and lines. And inserting bytes (characters) somewhere in the middle of the stream inevitably implies shifting all the other remaining bytes by the number of inserted bytes. But shifting a byte means another byte overwriting.
In case you are okay with shifting, you always can map the file to memory and operate with the content as if it would be a byte array.
Oh, do you know how to insert a byte into byte array?
Update:
Is it possible Using visual studio VC++ ??
Sure it is. Of course, if you know what to do, and how to do what you want to do.
Last edited by Igor Vartanov; May 20th, 2016 at 04:10 AM.
Re: Insert in a file without overwriting without using a 2nd file using C/C++
This is an existing text file and I want to replace one value(say 1234) with another value (say 12345) in the text file
Why not just use a text editor - or is this a homework assignment?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Originally Posted by 2kaud
Why not just use a text editor - or is this a homework assignment?
A GUI application will update the values in a name.ini file whenever the user changes the values in the controls. That's why i need to update the appropriate values in the name.ini file and not the whole file.
Re: Insert in a file without overwriting without using a 2nd file using C/C++
If you are using .ini files, why not use the WIN32 private profile functions? The format of the file is slightly different to what you showed in post #1 (colon replaced by =), but it is still a text file and you can use the Windows functions GetPrivateProfileString(), WritePrivateProfileString() etc. See https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx and links.
Note that it says for compatibility with 16-bit Windows applications, but they work fine with 32/64 bit.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Originally Posted by 2kaud
If you are using .ini files, why not use the WIN32 private profile functions? The format of the file is slightly different to what you showed in post #1 (colon replaced by =), but it is still a text file and you can use the Windows functions GetPrivateProfileString(), WritePrivateProfileString() etc. See https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx and links.
Note that it says for compatibility with 16-bit Windows applications, but they work fine with 32/64 bit.
Thanks for your answer!
Using WritePrivateProfileString(), is it possible just to replace just one string(value) without overwriting other / all key and strings???
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Originally Posted by GiriPrakash
Thanks for your answer!
Using WritePrivateProfileString(), is it possible just to replace just one string(value) without overwriting other / all key and strings???
I do not understand your being obsessed by overwriting something, really. Why don't you just read the proposed article and try to use the API, and make sure it does exactly what you want and the way you want it? Isn't that more reliable than asking people you never knew, and most probably never will do, about trivial things clearly explained in documentation? Why do you prefer plain talking-about to building your own experience?
This is how I would go, if it were my concern. Typically I create a simplistic app focused on the aspect I'm interested in. The app should be done the way flexible enough to test various aspects of the researched feature application:
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Using WritePrivateProfileString(), is it possible just to replace just one string(value) without overwriting other / all key and strings???
Yes!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Re: Insert in a file without overwriting without using a 2nd file using C/C++
Developed the test a little bit:
Code:
@echo off
122.exe myApp key1 value1
echo Added key1 record to [myApp] section
type 122.ini
122.exe myApp key2 value2
echo Added key2 record to [myApp] section
type 122.ini
122.exe myApp key1 value11
echo Modified key1 record in [myApp] section
type 122.ini
122.exe myOtherApp key1 value1
echo Added key1 record to [myOtherApp] section
type 122.ini
122.exe myOtherApp key1 value12
echo Modified key1 record in [myOtherApp] section
type 122.ini
122.exe myApp key1
echo Deleted key1 record from [myApp] section
type 122.ini
122.exe myApp
echo Deleted [myApp] section
type 122.ini
Code:
Added key1 record to [myApp] section
[myApp]
key1=value1
Added key2 record to [myApp] section
[myApp]
key1=value1
key2=value2
Modified key1 record in [myApp] section
[myApp]
key1=value11
key2=value2
Added key1 record to [myOtherApp] section
[myApp]
key1=value11
key2=value2
[myOtherApp]
key1=value1
Modified key1 record in [myOtherApp] section
[myApp]
key1=value11
key2=value2
[myOtherApp]
key1=value12
Deleted key1 record from [myApp] section
[myApp]
key2=value2
[myOtherApp]
key1=value12
Deleted [myApp] section
[myOtherApp]
key1=value12
Last edited by Igor Vartanov; May 31st, 2016 at 05:07 AM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.