Thread: Help with strcpy and arrays

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    Help with strcpy and arrays

    Hi

    Im kinda new to this but here we go...
    How is it possible to copy a text string to an array
    I got this huge xml file where there are alot of cars.
    I want to make an Array which would consist of car names...

    Code:
    /*Example:
    
    <car>Audi A2 1.4 55 kW</car>   
    
    */
    
    char *separate;
    char *car[2000];
    char row[2000];
    int m=0;
    
    //file opening procedure is also here.. thats not necessary right now.
    
    
    while(!feof(filename)){
    fgets(row, sizeof(row), filename);
    
    
    separate=strtok(row, "<>/n");
    do{
    
    /*So if the separated text string is car, I would like to copy the NEXT seperated text string into an Array... but unfortunately an application error jumps out....
    The instruction at "blablabla" referenced memory at"blablabla". The memory coulnd not be "written". Click ok to terminate the program*/
    
    if(!strcmp(separate, "car")){strcpy(car[m],strtok(NULL, "<"));m++}     //my bets, mistake is there <<
    
    }while(separate=strtok(NULL, "<>\n");       // < untill the row ends
    
    }
    I hope u understood.... if not, tell me... ill try to explain more.
    Last edited by doyle96; 05-19-2007 at 08:24 AM. Reason: forgot to declare m

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first call to strtok needs the string you want to tokenise.
    Subsequence calls to get the next token in the same string pass NULL.

    Why not use a library which understands XML (eg. expat) so you can focus on the data itself?

    Oh, and see the FAQ on why using feof() to control a loop is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Instead of using char arrays to store that information, why not just use vectors for each car name?

    Maybe that would make it easier i dunno.

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    2
    thx guys but I got a good tip from another forum ) which helped alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D arrays, strcpy and strcmp Problems
    By Windu102 in forum C Programming
    Replies: 3
    Last Post: 08-23-2008, 01:00 AM
  2. Using strcpy() and arrays of structs
    By vital101 in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 09:04 PM
  3. strcpy error with arrays
    By trang in forum C Programming
    Replies: 4
    Last Post: 01-10-2004, 10:13 PM
  4. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 10:46 PM
  5. help with strcpy and arrays with header file
    By Agnesa in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 05:06 PM