943,771 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2422
  • C RSS
Jan 6th, 2009
0

Append a Character(backslash) to a string at any position

Expand Post »
hi .

i have read a line from a text file & stored it in a string using fgets().

the file contains the following line:
C:\Windows\

  1. char myline[20];
  2. fgets(myline,20,fp);

now i want to append '\' in the string so that when i print myline output is

C:\\Windows\\

Any idea or algo how to do this in C ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Jan 6th, 2009
0

Re: Append a Character(backslash) to a string at any position

copy the string one character at a time into another variable that is long enough to hold the extra characters. When an '\' is encountered just copy two of them.

But I have to question your reason for doing that. When you type a path from the keyboard into a variable you don't need the couble '\' characters. They are only needed when you use string literals in your program because the compiler has to interpret the characters.
Last edited by Ancient Dragon; Jan 6th, 2009 at 10:05 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jan 6th, 2009
0

Re: Append a Character(backslash) to a string at any position

OK.
Thanks for the suggestion.
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: can't copy existing file into new file
Next Thread in C Forum Timeline: Optimized quicksort implementation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC