Thread: getting characters from a string that...

  1. #1
    Unregistered
    Guest

    Question getting characters from a string that...

    has been put into a variable. I need some advice or help on how to get the first two characters from what has been put into a variable using sscanf. I know it doesn't matter how the data was put there but I thought I would mention it anyway.

    partial example of my code that works except what I need to do:

    void main()
    {

    #define MAXLINE 128

    FILE *fr;

    char s5[10];
    char line[MAXLINE];

    if(fgets(line, MAXLINE, fr) != NULL) {
    sccanf (line, "%6c", s5);
    pId->Wx_Id = s5;

    // Here is what I don't know how to do
    pId->Temp = first 2 characters in s5

    exit(0);
    }

    }


    Thanks for your help!
    Alexsis

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: getting characters from a string that...

    >void main()
    Wrong, use int main(void), as main returns an int.

    >FILE *fr;
    >if(fgets(line, MAXLINE, fr) != NULL) {
    At what point did you open fr?

    If you want to copy n chars from one array to another, lookup the strncpy() function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    Sorry I didn't put all the code in. I have the file opened and I am reading it correctly. I just don't know how to get the first 2 characters from the one variable into the other.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >I just don't know how to get the first 2 characters from the one variable into the other.
    If you want to copy n chars from one array to another, lookup the strncpy() function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest

    Smile

    Hammer,
    Thank you for your help. I used the strncpy function and it did exactly what I wanted. Also, I visit this board often and review several posts each day, and without a doubt you offer the best help. You don't just give away answers you make people think (but you do it in a way that does not offend others...unlike Salem's responses).
    Cheers!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    :::blush:::
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, just to ensure you fully understand how to use strncpy(), here's a small, but complete, program.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
         char name[] = "Hammer";
         char twochars[3];
         
         strncpy (twochars, name, 2);
         
         printf ("%s", twochars);
         
         return 0;
    }
    It is supposed to print out the first two characters, eg Ha. But it has a bug, can you see it?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM