how to assign text to its own variable

This is a discussion on how to assign text to its own variable within the C Programming forums, part of the General Programming Boards category; hello cboard! i am hoping to gain knowledge in C more and more now that i have joint this forum ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    how to assign text to its own variable

    hello cboard!
    i am hoping to gain knowledge in C more and more now that i have joint this forum

    i am having a problem with one of my char variables (i know it will sound quite basic but i am more on the beginning side of things)

    i have stored within this char variable what ever was in my buff. now as im guessing it is stored as an array.
    what i basically want to do is take the first 5characters of each line of code and assign it to its own char variable so i can print it to the screen or do what i like to it once i build on it in the not so distant future
    i would prefer to be given the right function to be used so i can try figuring it out

    i am thankful for all replies

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    char *strncat(char *dest, const char *src, size_t n);

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There are a few functions you can use... memcpy() strncpy() being the two most common.

    Just remember to make your char arrays the length you want +1 and add the trailing null when done.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    thanks for the quick replies

    i have also got to keep an eye out for the future development of this program and because it will get to being sometimes more than and sometimes less than 5characters. i need a method which can extract it upto either it hits a certain number or character everytime
    this file does not have any null's so it would have to be a specific character or number. for example. everytime it comes accross an a it will save what it has seen before it into a variable then carry on until the next a and so forth

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stephen101 View Post
    thanks for the quick replies

    i have also got to keep an eye out for the future development of this program and because it will get to being sometimes more than and sometimes less than 5characters. i need a method which can extract it upto either it hits a certain number or character everytime
    this file does not have any null's so it would have to be a specific character or number. for example. everytime it comes accross an a it will save what it has seen before it into a variable then carry on until the next a and so forth
    This sounds pretty easily done... once you have the data in a buffer, strchr() will help you find your key, memcpy() or strncpy() will help you copy the data out... from there, well it's going to be up to what you need to do with this information....

    If there is a known format at work fscanf() and fprinf() might also be helpful.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    mm ok thanks loads im going to give this a shot and see where i can get

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stephen101 View Post
    mm ok thanks loads im going to give this a shot and see where i can get
    Well, if you run into problems you can always post up a code fragment (in CODE /CODE tags, please) and we'll see what we can do...

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    ok well i used the below line and then did a printf showing me that it had worked and i had the the error message:

    warning: multi-character character constant

    Code:
    char *pos = strchr(buffer, 'a');
    
    printf("a is found at %d\n", buffer-pos);

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stephen101 View Post
    ok well i used the below line and then did a printf showing me that it had worked and i had the the error message:

    warning: multi-character character constant

    Code:
    char *pos = strchr(heading, 'a');
    What's in heading?

    The code looks ok...

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    ok no ive got it
    i had changed it a couple of times

    heading had buffer stored within it but i thought its best to use buffer for now

    because my compiler does not like me using accented characters that message had come up!
    is there a way i could get my compiler to allow me to use characters such as , or ?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by stephen101 View Post
    ok no ive got it
    i had changed it a couple of times

    heading had buffer stored within it but i thought its best to use buffer for now

    because my compiler does not like me using accented characters that message had come up!
    is there a way i could get my compiler to allow me to use characters such as , or ?
    You should be able to use some accented characters -- anything that fits in a byte. By default, C uses one-byte characters (using whatever encoding is native to your machine). So for instance, if you are on a Windows PC, any character on the "Extended ASCII" list should be okay -- I can type into a program by pressing Alt+157(on the number pad only) and wahey there it is. If you try to use Unicode, then you've got a whole new can of worms.

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    hmm unicode..
    how can this be checked and what procedures should i take?
    i am running ubuntu 10.04 in the UK if the keyboard makes any difference at all!

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    To expand on what tabstop just said, without getting into multibyte strings you have to
    a) make sure every character you write fits into a solitary byte and
    b) make sure all the characters you need are available in ASCII (most likely for a given computer)
    c) enter the characters by code if you need to. For example \x64 might mean a particular character (expressed in hexadecimal)

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is your instructor expecting everyone to be on Linux? Once you get past 7 bits, all bets are off, really, when it comes to the extra characters. For instance, on my Windows machine, typing
    Code:
    printf("%c\n", 157);
    gives me , while on my Mac it just gives me ?. (In fact, my Mac prints ? for every character between 128 and 255.) (I haven't bothered to reinstall Ubuntu since the last upgrade horribly failed, so I can't check there.)

    So you will need to determine (visually/in a hex editor/by reading and printing out) what value your marker character actually has.

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    i replaced the special character for a 'R' just because i wanted to see if it was working and... i had a set back of it not creating my file with the name. my code is below
    Code:
    char *pos = strchr(buffer, 'R');
    
    if (pos)
    printf("R is found at %d\n", buffer-pos);
          
    nwrite = open(buffer-*pos, O_CREAT, S_IRUSR|S_IWUSR);
    the code compiles well enough just doesnt create the file for some odd reason!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes when trying to assign a value to a class variable
    By verxintRising in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2010, 02:32 AM
  2. read a text file with lines of variable length
    By raymond in forum C Programming
    Replies: 7
    Last Post: 06-24-2006, 06:41 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  4. Writing a member variable to a text file
    By Onions in forum C++ Programming
    Replies: 3
    Last Post: 11-08-2005, 09:26 AM
  5. Replies: 3
    Last Post: 05-25-2005, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21