2 dimension array of characters

This is a discussion on 2 dimension array of characters within the C Programming forums, part of the General Programming Boards category; Hi , I am stuck with two dimension array of characters. for example, I have 4 lines in a file( ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    2 dimension array of characters

    Hi , I am stuck with two dimension array of characters.

    for example, I have 4 lines in a file( lines should be treated as arrays of string) called source:

    i am not good at C.
    i hope improve soon.
    i need help.
    it is very cold.

    I want read the first two lines and store them in a new 2-dimension array of characters called destination.

    I know how to read (all the lines but not the first two) but when I tried to input the line in destination I get errors: I tried strcpy, memcpy...

    What can I do please?
    B

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a loop. Read a line into a buffer. Do something with it. Repeat until you feel like stopping. Consider fgets. Also consult the FAQ section of the site.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I know how to read (all the lines but not the first two) but when I tried to input the line in destination I get errors: I tried strcpy, memcpy...

    What can I do please?
    post the relevant code if the faqs don't help

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by MadCow257
    post the relevant code if the faqs don't help
    Ok this is my code:
    Code:
     while(fgets(line,19,fin)!=NULL) {/* while begins*/
              for(i=0;i<2;i++)
             {            
                strcpy(destination[i],source[i]);/*this is where I mess up*/
               fputs(source,fout);
             }
    for(i=0;i<19;i++)
    fputs(destnation[i],fout);}
    My idea is to copy what I read from the file and put it in an array.
    whem I try to introduce the double subscript like destination [i][j], it still give me error.
    please, I need help since I dont suceed to find a solution.

    Thank you
    B

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    How is destination and source declarated? And what does source contain? What is the error you're getting?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by OnionKnight
    How is destination and source declarated? And what does source contain? What is the error you're getting?
    This is how I declared the arrays.

    char source[20];
    char destination[2][20];

    My problem is that I read a numer of line from an input file.
    I decided to called source each line read from the file.

    I get the error: [Warning] passing arg 1 of `strcpy' makes pointer from integer without a cast

    I tried a lot of comination but I get basically the same error.

    thank you

  7. #7
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Quote Originally Posted by braddy
    My problem is that I read a numer of line from an input file.
    I decided to called source each line read from the file.
    Code:
     while(fgets(line,19,fin)!=NULL) {/* while begins*/
    In your code you are reading each line into a variable called line but I don't see you putting the contents of line into source anywhere.

    Code:
    strcpy(destination[i],source[i]);/*this is where I mess up*/
    source[i] is of type char. What you wanted was probably
    Code:
    strcpy(destination[i],source);/*this is where I mess up*/

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by OnionKnight
    Code:
     while(fgets(line,19,fin)!=NULL) {/* while begins*/
    In your code you are reading each line into a variable called line but I don't see you putting the contents of line into source anywhere.

    Code:
    strcpy(destination[i],source[i]);/*this is where I mess up*/
    source[i] is of type char. What you wanted was probably
    Code:
    strcpy(destination[i],source);/*this is where I mess up*/

    Code:
     while(fgets(line,19,fin)!=NULL) {/* while begins*/
    line is a typo I meant to write source instead.

    I just try:
    Code:
    strcpy(destination[i],source);/*this is where I mess up*/
    [/QUOTE]
    but it still gives me error

    my problem is to create this 2-D array of characters.Besides strcpy there are other way to do it?

    Thank you

  9. #9
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Well, there's a lot of strange things going on in your code.
    Code:
     while(fgets(line,19,fin)!=NULL) {/* while begins*/
              for(i=0;i<2;i++)
             {            
                strcpy(destination[i],source[i]);/*this is where I mess up*/
               fputs(source,fout);
             }
    for(i=0;i<19;i++)
    fputs(destnation[i],fout);}
    For each line you're reading that line gets copied into the two buffers of destination and the line gets written to fout two times. After that destination seems to be storing 19 buffers instead of 2 and those two buffers plus the mysterious potentially segfaulting 17 others gets written to fout.
    There are other way than strcpy but they don't differ much and strcpy doesn't seem to be your problem.

    [edit]
    I need to get my ass to school so let's get down to brass tacks, I think you want something like this.
    Code:
    for (i = 0; i < 2; i++) {
        if (fgets(source, 20, fin) != NULL) {
            strcpy(destination[i], source);
            fputs(source, fout);
        }
        else {
            //errors
        }
    }
    You can also skip the use of source entirely by reading directly into destination e.g. fgets(destination[i], 20, fin)
    Last edited by OnionKnight; 03-15-2006 at 01:57 AM.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    39
    Quote Originally Posted by OnionKnight
    Well, there's a lot of strange things going on in your code.
    Code:
     while(fgets(line,19,fin)!=NULL) {/* while begins*/
              for(i=0;i<2;i++)
             {            
                strcpy(destination[i],source[i]);/*this is where I mess up*/
               fputs(source,fout);
             }
    for(i=0;i<19;i++)
    fputs(destnation[i],fout);}
    For each line you're reading that line gets copied into the two buffers of destination and the line gets written to fout two times. After that destination seems to be storing 19 buffers instead of 2 and those two buffers plus the mysterious potentially segfaulting 17 others gets written to fout.
    There are other way than strcpy but they don't differ much and strcpy doesn't seem to be your problem.

    [edit]
    I need to get my ass to school so let's get down to brass tacks, I think you want something like this.
    Code:
    for (i = 0; i < 2; i++) {
        if (fgets(source, 20, fin) != NULL) {
            strcpy(destination[i], source);
            fputs(source, fout);
        }
        else {
            //errors
        }
    }
    You can also skip the use of source entirely by reading directly into destination e.g. fgets(destination[i], 20, fin)

    Thank you very much!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Replies: 7
    Last Post: 05-11-2008, 11:57 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. 2 dimension string array
    By djarian in forum C Programming
    Replies: 4
    Last Post: 05-05-2003, 01:47 AM
  5. Can a string be converted to an array of characters?
    By supaben34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2002, 12:18 PM

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