Help using strcpy

This is a discussion on Help using strcpy within the C Programming forums, part of the General Programming Boards category; i am trying to copy a string that i got from a file using fscanf into my struct Code: struct ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    Help using strcpy

    i am trying to copy a string that i got from a file using fscanf into my struct

    Code:
    struct Room {
      char south, north, east, west;
      char id;
      char description[MAX_SIZE_DESC];
      char name[MAX_SIZE_NAME];
    };
    
    int buildRooms(char filename[], struct Room list[])
    {
      char id[2];
      char roomname [20];
      char north[2];
      char east[2];
      char west[2];
      char south[2];
      char sentence [100];
      char space[4];
      FILE *f = fopen(filename, "r");
      if (f == NULL) {
        fprintf(stderr, "Error opening game file %s./n", filename);
        return 42;}              // failure                                                                    
      else{
          fscanf(f,"%s%s%s%s%s%s",id, roomname, north, east, west, south);
          fgets(space, sizeof(space),f);
          fgets(sentence, sizeof(sentence),f);
          struct Room list;
          strcpy(list.id, id);
      }
     fclose(f);
      return 0;                   // success                                                                   
    }
    i am having trouble in the first strcpy it is saying that i am passing the first argument of strcpy makes pointer from integer without a cast

    any suggestions

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Maybe because you're trying to copy a char array into a single char?

    You could try changing your struct to this:

    Code:
    struct Room {
      char south, north, east, west;
      char id[2];
      char description[MAX_SIZE_DESC];
      char name[MAX_SIZE_NAME];
    };
    However you will have to make some changes to the south, north, east, and west variables.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by mherald81 View Post
    Maybe because you're trying to copy a char array into a single char?

    You could try changing your struct to this:

    Code:
    struct Room {
      char south, north, east, west;
      char id[2];
      char description[MAX_SIZE_DESC];
      char name[MAX_SIZE_NAME];
    };
    However you will have to make some changes to the south, north, east, and west variables.
    that part was given to us by our instructor so were not allowed to change it is there another way?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Since you only want the first char,
    list.id = id[0];
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2010, 08:42 AM
  2. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  3. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM

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