String manipulation

This is a discussion on String manipulation within the C Programming forums, part of the General Programming Boards category; Hi I give my input through a file. so in my program i get the path of the file from ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    8

    String manipulation

    Hi
    I give my input through a file. so in my program i get the path of the file from user and manipulate the data in it. i want to place the output also in a file in the same place as my input file. for example my input file resides here G:\BC\BIN\input.txt i want the output file also in the same place like this G:\BC\BIN\output.txt

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok it seems you know all the file i/o stuff, correct? it then looks like what you have to do is figure out how to get the path to the input file given, ie everything in the input up to the last slash.

    how can you get the last occurrence of a slash (or any character)? read strrchr. after finding out where the last occurrence of a slash is, we know the path.

    say the input string was c:\file.txt. the last occurrence of a slash, is at index 2 (ie 3rd character). we can then create another string of size number of characters up to the last slash + length of output filename, in your example this would be 10 + 10 = 20 characters. concatenating the new-found path and your output filename should be easy.

    edit: also note its possible you dont have permission to write to a given path. if this is just a homework assignment on your home pc im sure that isnt an issue, though. another thing, make sure you prompt (or simply disallow) to overwrite a file if it already exists, to avoid any silly mistakes.
    Last edited by nadroj; 01-15-2008 at 09:12 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Raghavan View Post
    Hi
    I give my input through a file. so in my program i get the path of the file from user and manipulate the data in it. i want to place the output also in a file in the same place as my input file. for example my input file resides here G:\BC\BIN\input.txt i want the output file also in the same place like this G:\BC\BIN\output.txt
    It might be easier to just ask the user to put your program and his input file, into the same directory. You're using QuickBasic, right?

    A while loop can also find that path, if that's not possible:
    Code:
    i = len(pathWithFilestring$
    
    while (mid$(pathWithFilestring$, i, 1) <> "\")  
       i = i - 1
    wend
    
    'Now you have the last backslash in the path, which is also it's last character before the filename.
    'getting the rest of the path
    path$ = left$(pathWithFilestring$, i)
    
    'now add the filename:
    path$ = path$ + "output.txt"
    
    'check it
    print path$
    It will be 1 based, not zero-based.

    That's off the cuff, but you get the idea. If you want more help with it, let me know. I have QuickBasic, still.
    Last edited by Adak; 01-15-2008 at 09:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:41 AM

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