Results 1 to 10 of 10

Thread: letter onto a string

  1. #1

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353

    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.

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    try sprintf its the same syntax as strcat

  3. #3

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    No thats not working either. I'm getting the error of Cannot convert 'int' to const char *'
    Any more ideas anyone?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  6. #6

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    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.

  7. #7

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    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?

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  9. #9

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    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);
    }

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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
  •  



Click Here to Expand Forum to Full Width