|
-
May 9th, 2001, 02:09 PM
#1
Thread Starter
Hyperactive Member
letter onto a string
Should be quite simple this...
I'm doing some C coursework (using Borland), and I've read a character from a text file. I now want to put the letter onto the end of the string, but everytime I keep getting the same error. I've tried using strcat but that doesn't work either.
Heres the basics of the code
char letter;
char reading[30];
FILE *fp;
fp=fopen("names.txt","r");
letter=fgetc(fp);
strcat(reading,letter);
Can anybody tell me what the problem is and how I can get this working.
-
May 9th, 2001, 02:27 PM
#2
Frenzied Member
try sprintf its the same syntax as strcat
-
May 9th, 2001, 03:09 PM
#3
Thread Starter
Hyperactive Member
No thats not working either. I'm getting the error of Cannot convert 'int' to const char *'
Any more ideas anyone?
-
May 9th, 2001, 03:12 PM
#4
Monday Morning Lunatic
Code:
char letter;
char reading[30];
FILE *fp;
fp = fopen("names.txt", "r");
letter = fgetc(fp);
sprintf(reading, "%c", letter);
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 9th, 2001, 03:13 PM
#5
Frenzied Member
strcat() isn't working because it's expecting two pointers to strings as parameters, so it can concatenate the second string onto the end of te first one. What you are passing into the function needs to be of type char *, and you are passing a single character, of type char.
Also, the char[] variable you have called 'reading' should be initialised with a string. The strcat function will look for the end of the string so it knows where to start addign bits on, and if you haven't initialised it with a string you can't be sure where, or if, there will be an end-of-string character (ASCII code of 0, the character '\0').
sprintf() is probably much easier to use for most purposes. It works just like printf() but the first argument is the string variable to print the formatted string to.
Anyway, I don't know if you're assigning an initial string to reading. If you want to assign a value to it you can use the strcpy() funtion.
Harry.
"From one thing, know ten thousand things."
-
May 9th, 2001, 03:23 PM
#6
Thread Starter
Hyperactive Member
Thanks everyone for your help. Thats got it sorted, so I'll carry on doing my project, until the next thing goes wrong (probably in about 10 minutes :-( ), when it all goes wrong again.
Thanks again.
-
May 9th, 2001, 04:03 PM
#7
Thread Starter
Hyperactive Member
Bad news people.
I've just tried my code out and the sprintf seems to be overwriting the string rather than adding the letter onto the end. any ideas?
-
May 9th, 2001, 04:06 PM
#8
Monday Morning Lunatic
It's putting it at the start. Since your code doesn't seem to load anything from the file, there's nothing else to put down
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 9th, 2001, 04:11 PM
#9
Thread Starter
Hyperactive Member
This is my code rattled down a little. On the puts line it is only outputting the last letter that its loaded from the file.
e.g.
T
e
s
t
where it should be doing
T
Te
Tes
Test
{shouldn't it?}
while(feof(fp)==0)
{
letter=fgetc(fp);
sprintf(reading,"%c",letter);
puts(reading);
}
-
May 9th, 2001, 05:15 PM
#10
Frenzied Member
This is why I mentioned the fact that you aren't initialising your string. sprintf() just overwrites whatever you have in the string already. You can get around this by writing what's already in the string and then something else:
Code:
while(feof(fp)==0)
{
letter=fgetc(fp);
sprintf(reading,"%s%c", reading, letter);
puts(reading);
}
You still need to initialise the string and make it empty to begin with though. Just change your declaration to this:
Code:
char reading[30] = "";
or this
Code:
char reading[30];
reading[0] = '\0';
PS please use the [code][/code] tags to format your code, it's much much easier to read.
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|