Need a little help with file handeling

This is a discussion on Need a little help with file handeling within the C++ Programming forums, part of the General Programming Boards category; Hi I have partly made a program that is supposed to be able to execute a search and replace action ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    19

    Question Need a little help with file handeling

    Hi

    I have partly made a program that is supposed to be able to execute a search and replace action from lets say a batch file or a shortcut.
    I have managed to make the program accept the inputs, so basicly all i need now is the search and replace function

    I have never done anything with files before (ok, i have used a_file<<" "; to write to a file, but thats not exactly what im after) so i basicly dont know where to look.

    Does someone know a good tutorials for file handeling ( preferably something that includes something relevant to what im after ) or just file handeling in general.

    My compiler is Dev-C++ and this program only needs to work on windows (xp) which is the OS im using.

    Im sorry if this has been answered before, i used the search function, but couldent find anything relevant

    Thanks in advance.

    ~DumAzz~

    P.S (Yes, im new to c++)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are tutorials on the homepage of this site.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39
    You can check here to get a feel for how to use ofstream. I believe you should be able to use seekp to set the position of the file pointer then write to write there.

    There are examples there to help you out as well.

    I hope I was of help,

    cs_student.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Quote Originally Posted by matsp View Post
    There are tutorials on the homepage of this site.

    --
    Mats
    I have gone through that tutorial, but thanks anyway

    Quote Originally Posted by cs_student View Post
    You can check here to get a feel for how to use ofstream. I believe you should be able to use seekp to set the position of the file pointer then write to write there.

    There are examples there to help you out as well.

    I hope I was of help,

    cs_student.
    Thanks cs_student, that page contained A LOT of good material i can use however, i didnt find a function that allows me to search for a string.




    How do you search for a string and where is in the word are you after you have searched (infront of it or behind it?)? I need a search function sinced i dont know where in the file the word is.

    Thanks
    ~DumAzz~
    Last edited by dumazz; 08-04-2008 at 02:39 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You don't search for a string in a file, you read the file contents in and search while reading or search the contents after you have saved them somewhere.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Code:
    open input file for reading
    open temporary output file for writing
    
    while you can read input file line into variable:
        check variable for position of search string 
          (how you do this depends on whether you're using C-style character arrays or C++ std::string objects)
        if search string is not found:
          write contents of variable to file
        else:
          write contents of variable, up to, but not including the found location, to file
          write replacement string to file
          move search string pointer to location immediately following search string
          write contents of remainder of variable to file
        end if
    end while loop
    
    close both files
    copy temporary file over original file
    Last edited by rags_to_riches; 08-04-2008 at 02:54 PM. Reason: Formatting sucked.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Quote Originally Posted by rags_to_riches View Post
    Code:
    open input file for reading
    open temporary output file for writing
    
    while you can read input file line into variable:
        check variable for position of search string 
          (how you do this depends on whether you're using C-style character arrays or C++ std::string objects)
        if search string is not found:
          write contents of variable to file
        else:
          write contents of variable, up to, but not including the found location, to file
          write replacement string to file
          move search string pointer to location immediately following search string
          write contents of remainder of variable to file
        end if
    end while loop
    
    close both files
    copy temporary file over original file
    Hmm, i understand how that would be a good way to make it, but i still need to find out how to do the different tasks. well, i guess ill go one step at the time

    Quote Originally Posted by rags_to_riches View Post
    Code:
     
    while you can read input file line into variable:
    First i input whole file into a string? (or can you put anything else then numbers in variables?) And the file might be multiple lines, but i guess that dosent matter?

    Quote Originally Posted by rags_to_riches View Post
    Code:
     
    check variable for position of search string 
    (how you do this depends on whether you're using C-style character arrays or C++ std::string objects)
    i think im using c++ std::string objects, so how do i search in a string (where can i find it out)

    Quote Originally Posted by rags_to_riches View Post
    Code:
     
    move search string pointer to location immediately following search string
          write contents of remainder of variable to file
    I guess i just loop the search for string untill eof (end of file (or string in this case))? since the document might contain more occurences of the word to search for.

    Im sorry if i seem slow, or annoying, but like i said, im kinda new to c++ and its late here , AND my head is spinning from all the reading ive done today. Bare with me

    ~Dumazz~

    Edit: im going to bed, i REALLY REALLY NEED IT...

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    That solution, by the way, assumes there would only be one match per line of input.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Here's a good place to get started: C++ Reference

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Quote Originally Posted by rags_to_riches View Post
    That solution, by the way, assumes there would only be one match per line of input.
    i see, but i guess looping the search function would solve that problem.
    or if that dosent help, ill figure something else out....tomorrow
    Thanks for all the help so far

    ~Dumazz~

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    Hey.

    Ok i have managed to put the first line of the file into a string / character array.

    how do i search through a/the string/ character array?

    ~DumAzz~
    Last edited by dumazz; 08-05-2008 at 03:29 AM.

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Everything you need is on the C++ Reference site to which I linked.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    i found it now..
    Thanks for all your help, ill finnish up the project and post the content here when im done.
    Special thanks to cs_student for good link and VERY special to rags_to_riches for helping a lot.

    ~DumAzz~

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    19
    so..i finished the first version of the code (i know it took long, but i had some stuff to do...like my birthday ) anyway...i know it will error if you try to replace "test" with "test test" since it will find that 2nd one too, but i will fix that later (by searching for found+(argv[3]'s length).

    if there was anything else, i'dd be happy to get feedback
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21