#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0

    two dimensional character array


    hi, i was wondering if and how you can save a string to an array of strings. for example, the declaration reads : "char charname[3][20]={"","Player 1","Player 2"};" i want to update this later, and i can't get it to save another string to over one of the values.
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    This works for me:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char charname[3][20]={"","Player 1","Player 2"};
    
    int main()
    {
    	int i;
    	char *s1 = "string";
    	char *s2 = "abc";
    
    	strcpy(charname[0],s1);
    
    	for (i=0; i<3; i++)
    		printf("%s\n",charname[i]);
    
    	strcpy(charname[1],s2);
    
    	for (i=0; i<3; i++)
    		printf("%s\n",charname[i]);
    
    	return(0);
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0
    great, thanks a bunch jason
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0
    im still sort of having a problem. i want the strings to be copied to the array to be variables, because i want to read in information from a file. when i try to use the code you provided, i would get something like 2 lines of garbage. the code is below:

    void loadstats(fstream& load)
    {
    char temp[20];
    char temp1[20];
    char name[21]="save.txt";
    load.open(name,ios::in|ios::out|ios::noreplace);
    if (load.fail())
    load.open(name,ios::out|ios::in);
    else
    {
    cout<<"Enter Player 1 name: ";
    cin>>players[1];
    cout<<"Enter Player 2 name: ";
    cin>>players[2];
    load<<players[1]<<endl<<players[2];

    }



    load>>temp>>temp1;

    strcpy(players[1],temp);
    strcpy(players[2],temp1);
    }

    where players is a 3*20 character array. when i try and run the program with this code i get a line of garbage where each name is supposed to be.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    char temp[20];
    char temp1[20];

    You declare temp and temp1 without intializing them, so they contain junk values. Then you do this:

    strcpy(players[1],temp);
    strcpy(players[2],temp1);

    which copies temp and temp 1 into players[1] and players[2]. So, you end up with junk values in players[1] and players[2]. One reason you should always intialize variables when you declare them is so you can catch errors like that easier. In this case, if you initialized the variables like so:

    char temp[20]={0};
    char temp1[20]={0};

    you would have seen that players[1] and players[2] were empty, and hopefully that would have made it easier for you to understand what went wrong.
    Last edited by 7stud; April 18th, 2003 at 05:12 PM.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0
    well, the whole problem i am having is that i am trying to store values read from the file to temp and temp1, and then store these to the array. when i initialized the temp values, nothing got stored to them when i tried to read from the 'load' stream. i would just get the same data i initialized to the temp variables. i don't understand!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Why would you need to read data into a temp variable and then assign it to another variable? It sounds like you might not understand how to read input from a file. Here's the basic format:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	char players[3][20]={
    		"Hello World",
    		"Goodbye World",
    		"I think I can."
    		};
    	ifstream inFile("C:\\TestData\\input.txt");
    	
    	for(int i=0; i<3; i++)
    	{
    		inFile>>players[i];
    	}
    
    	for(i=0; i<3; i++)
    	{
    		cout<<players[i]<<endl;
    	}
    
    	return 0;
    }
    Last edited by 7stud; April 18th, 2003 at 09:48 PM.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0
    maybe im just retarded, but for some reason this line isn't reading the file. i have load to be an fstream in 'ios::in|ios::out' mode, so this should work!:
    load>>players[1]>>players[2];


    some reason, this is not working, can you please let me know what is so wrong about it! then i do a tellg before and after this line, it says 0 and 0, can you please shed some light on me. the whole function was posted earlier

    and the declaration for the global variable 'players' is : char players[3][20]={"","P1","P2"};

    ps, what does "using namespace std" do? and why does it not compile sometimes in visual c++?
    Last edited by airswit; April 19th, 2003 at 12:02 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    It looks like you are writing players[1] and players[2] to the file and then immediately trying to read them from the file back into temp1 and temp2. The problem is that the current position of load is at the end of the file after the two lines you just wrote to it. Try repositioning load so that it is back at the beginning of the file. Does this fail if the file already exists?
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0
    the code as i have it now, only writes to the file when there was no file of that name, and it reads otherwise. and as i stated before when i did a tellg and the value it returned was 0 both before and after the read from the file. if any of you want to see the whole file, just let me know, but im still having problems, and when i initialize the strings they don't get updated when i try to read the file into them
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0
    you know, i think there is some problem with how i am opening the file, because when i try to open it after the noreplace fails, the regular trunc open fails right after, even if i close the stream before i attempt to open it back up:
    Code:
    fstream load;
    char name[21]="save.txt";
    load.open(name,ios::in|ios::out|ios::noreplace);
    if (load.fail())
    {
    	load.close();
            load.open(name,ios::in|ios::trunc);	
    		if (load.fail())
    		cout<<"this is messed up!";
    }
    
    else
    {
    	cout<<"Enter Player 1 name: ";
    	cin>>players[1];
    	cout<<"Enter Player 2 name: ";
    	cin>>players[2];
    	load<<players[1]<<endl<<players[2];
    }
    can anyone tell me what i am doing wrong? the error message shows up when i run this code!
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "i did a tellg and the value it returned was 0 both before and after the read from the file."

    That means the file you opened is empty. Your path to the file you want to open is probably wrong, so your program created a new empty file. Where is the file located on your computer? You need to use the full path name and double the slashes, for example:

    "C:\\My Documents\\save.txt"

    In addition, you do not have to worry about the file mode--the default mode is fine. (Why are you opening a file for both reading and writing when all you want to do is read?) The code I posted works just as well with an fstream object--only one line has to be changed:

    fstream inFile("C:\\TestData\\input.txt");
    Last edited by 7stud; April 19th, 2003 at 04:25 AM.
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    19
    Rep Power
    0
    i don't specify any directory, which just saves it in the same dir as the program itself. i open it up as an fstream in and out because later in the function i read from it, hence the ios::in|ios::out. this is not the whole function
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    I suggest you set your current program aside, and start a short practice program and learn to do basic file I/0. Play around with the code I posted until you figure out how it works, then alter it to try some other things, and then you should be able to incorporate what you learned into your current program.

    "what does "using namespace std" do?

    Read some beginning C++ tutorials on namespaces.
    Last edited by 7stud; April 19th, 2003 at 04:36 AM.
  28. #15
  29. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    1
    Rep Power
    0
    airswit, I agree with 7stud - try practicing reading and writing to files in a new program to get the hang of it.
    Here are some suggestions to help you on your way:
    1) You probably want to declare separate streams for reading and writing to clear up any confusion
    ifstream loadfile;
    ofstream writefile;

    2) Try just using the default settings for open
    loadfile.open("save.txt")

    3) Check for failure to open straight away and only once
    if(loadfile==NULL){
    //cout some error message
    exit (-1)
    };

    4) Try a skeleton function that doesnt do anything and see if you get the failure message from 3 above. If you dont then you can add the rest of your code in

    Hope this helps ...

IMN logo majestic logo threadwatch logo seochat tools logo