The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How do I reset a char variable back to its original state?
Discuss How do I reset a char variable back to its original state? in the C Programming forum on Dev Shed. How do I reset a char variable back to its original state? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 27th, 2011, 07:18 PM
|
|
Registered User
|
|
Join Date: Apr 2010
Location: Location : Location : Location
Posts: 63
Time spent in forums: 17 h 57 m 39 sec
Reputation Power: 0
|
|
|
how do i set char? (stucked here for 2 hours)
Let me rephrase my question.
Example code:
Code:
char fileName[128] = "essay";
char fileNumber[128];
char *fileFormat = ".doc";
itoa(0, fileNumber, 10);
strcat(fileName, fileNumber);
strcat(fileName, fileFormat);
printf("%s\n", fileName);
itoa(1, fileNumber, 10);
strcat(fileName, fileNumber);
strcat(fileName, fileFormat);
printf("%s\n", fileName);
itoa(2, fileNumber, 10);
strcat(fileName, fileNumber);
strcat(fileName, fileFormat);
printf("%s\n", fileName);
Result:
Code:
essay0.doc
essay0.doc1.dat
essay0.doc1.dat2.dat
What I want is:
Code:
essay0.doc
essay1.doc
essay2.doc
Hope you can see what I mean, I need a method that can set fileName[128] back to JUST the word essay. I want to get rid of the old stuff(number and file format string) at behind.
|

May 27th, 2011, 07:50 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Unless you actually want to modify helloString every time, you can just print the two strings separately.
As far as output is concerned, your desired code is equivalent to
Code:
char helloString[128] = "Hello";
for (i = 0; i < 3; i++) {
printf("%s%d\n", helloString, i);
}
|

May 27th, 2011, 08:03 PM
|
|
Registered User
|
|
Join Date: Apr 2010
Location: Location : Location : Location
Posts: 63
Time spent in forums: 17 h 57 m 39 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by requinix Unless you actually want to modify helloString every time, you can just print the two strings separately.
As far as output is concerned, your desired code is equivalent to
Code:
char helloString[128] = "Hello";
for (i = 0; i < 3; i++) {
printf("%s%d\n", helloString, i);
}
|
hey sir, is there any method i can store this "("%s%d\n", helloString, i)" into a char? i need it as a file name, cuz i have 3 files, each named XXXfile1, XXXfile2, XXXfile3.
|

May 27th, 2011, 08:15 PM
|
|
|
|
Look up sprintf(). Use it to create a formatted string, and then printf() that string.
__________________
Right 98% of the time, and don't care about the other 3%.
“It has been said that the great scientific disciplines are examples of giants standing on the shoulders of other giants. It has also been said that the software industry is an example of midgets standing on the toes of other midgets.” (Alan Cooper)
|

May 27th, 2011, 10:10 PM
|
|
Registered User
|
|
Join Date: Apr 2010
Location: Location : Location : Location
Posts: 63
Time spent in forums: 17 h 57 m 39 sec
Reputation Power: 0
|
|
|
woot, strncpy() works.
|

May 28th, 2011, 02:14 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by Dakaa woot, strncpy() works. |
Yes of course it does, but that does not make it a good idea!
Code:
char fileName[32] ;
int i ;
for( i = 0; i < 3; i++ )
{
sprintf( "essay%d.doc", i ) ;
printf( "%s\n", fileName ) ;
}
For all sorts of cautions regarding string handling in C, especially strcat() read .
If you wanted to go with your original method then you could have also done this:
Code:
char fileName[128] = "essay";
char fileNumber[128];
char *fileFormat = ".doc";
char* truncate = &fileName[strlen(fileName)] ;
// reset fileName
*truncate = 0 ;
This will overwrite the first digit of the essay number with a string terminator. It is somewhat more efficient that a strcpy(), but more importantly truly sets the string to its original value, whereas with strcpy() you are copying new data that just happens to be the same as the original; more error prone perhaps, and certainly more of a maintenance issue.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|