Help with strcpy and arrays

This is a discussion on Help with strcpy and arrays within the C++ Programming forums, part of the General Programming Boards category; Hi Im kinda new to this but here we go... How is it possible to copy a text string to ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 09: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, 04:06 PM

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