CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2016
    Posts
    5

    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.

  2. #2
    Join Date
    Jan 2015
    Posts
    16

    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.

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    Quote Originally Posted by GiriPrakash View Post
    Is there any solution just to insert characters without overwriting the characters in the middle of the file.
    No. Period.
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2016
    Posts
    5

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    Quote Originally Posted by GiriPrakash View Post
    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

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    See the answers in the posts## 2,3,4.
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    Quote Originally Posted by GiriPrakash View Post
    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.
    Best regards,
    Igor

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    May 2016
    Posts
    5

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    Quote Originally Posted by 2kaud View Post
    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.

    Thanks for your reply

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    May 2016
    Posts
    5

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    Quote Originally Posted by 2kaud View Post
    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???

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    Quote Originally Posted by GiriPrakash View Post
    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:
    Code:
    // 122.cpp
    #include <windows.h>
    #include <tchar.h>
    
    static TCHAR iniFile[MAX_PATH] = {0};
    
    void GetIniFileName()
    {
        GetModuleFileName(NULL, iniFile, MAX_PATH);
        LPTSTR pExt = _tcsrchr(iniFile, TEXT('.'));
        _tcscpy(pExt, TEXT(".ini"));
    }
    
    int main()
    {
        GetIniFileName();
        WritePrivateProfileString(__argv[1], __argv[2], __argv[3], iniFile);
        return 0;
    }
    Then I create a number of cases covering the situations I would expect to be important for me, like adding new records or modifying existent ones:
    Code:
    J:\Temp\122>test.cmd
    
    
    J:\Temp\122>122.exe myApp key1 value1
    
    J:\Temp\122>echo.
    
    
    J:\Temp\122>type 122.ini
    [myApp]
    key1=value1
    
    J:\Temp\122>122.exe myApp key2 value2
    
    J:\Temp\122>echo.
    
    
    J:\Temp\122>type 122.ini
    [myApp]
    key1=value1
    key2=value2
    
    J:\Temp\122>122.exe myApp key1 value11
    
    J:\Temp\122>echo.
    
    
    J:\Temp\122>type 122.ini
    [myApp]
    key1=value11
    key2=value2
    
    J:\Temp\122>122.exe myOtherApp key1 value1
    
    J:\Temp\122>echo.
    
    
    J:\Temp\122>type 122.ini
    [myApp]
    key1=value11
    key2=value2
    [myOtherApp]
    key1=value1
    
    J:\Temp\122>122.exe myOtherApp key1 value12
    
    J:\Temp\122>echo.
    
    
    J:\Temp\122>type 122.ini
    [myApp]
    key1=value11
    key2=value2
    [myOtherApp]
    key1=value12
    Attached Files Attached Files
    Last edited by Igor Vartanov; May 30th, 2016 at 09:33 AM.
    Best regards,
    Igor

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Best regards,
    Igor

  15. #15
    Join Date
    May 2016
    Posts
    5

    Re: Insert in a file without overwriting without using a 2nd file using C/C++

    Quote Originally Posted by Igor Vartanov View Post
    Developed the test a little bit:
    Thank you so much for your reply!!
    Amazing logic you have used.

    Although i used .ini file to save the values and linked that file to windows registry.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured