C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 27th, 2011, 07:18 PM
Dakaa Dakaa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Location: Location : Location : Location
Posts: 63 Dakaa Negative: is most likely a SPAMMER and a traitor to the cause. 
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.

Reply With Quote
  #2  
Old May 27th, 2011, 07:50 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 53 m
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to 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);
}

Reply With Quote
  #3  
Old May 27th, 2011, 08:03 PM
Dakaa Dakaa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Location: Location : Location : Location
Posts: 63 Dakaa Negative: is most likely a SPAMMER and a traitor to the cause. 
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.

Reply With Quote
  #4  
Old May 27th, 2011, 08:15 PM
LittleGrin LittleGrin is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2007
Posts: 921 LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level)LittleGrin User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 11 h 50 m 18 sec
Reputation Power: 535
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)

Reply With Quote
  #5  
Old May 27th, 2011, 10:10 PM
Dakaa Dakaa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Location: Location : Location : Location
Posts: 63 Dakaa Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 17 h 57 m 39 sec
Reputation Power: 0
woot, strncpy() works.

Reply With Quote
  #6  
Old May 28th, 2011, 02:14 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,807 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 20 m 11 sec
Reputation Power: 1800
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
Code:
this
.

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How do I reset a char variable back to its original state?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap