Thread: Read char by char into two-dimensional array pointer.

  1. #1
    Registered User
    Join Date
    Sep 2012

    Read char by char into two-dimensional array pointer.

    Hello guys could you please help me write a piece of code that will read a file char by char and store it into the 2Dimensional array pointer using "File Pointer"?
    Here's my file:
    0.2.0 9.4.7 0.0.0
    0.5.0 0.0.6 0.4.7
    0.7.0 0.0.0 1.0.6

    7.4.0 3.1.0 0.0.5
    0.9.0 8.0.4 0.6.0
    5.0.8 0.6.0 0.9.3

    0.0.4 0.0.0 0.7.0
    0.8.0 2.0.0 0.3.0
    0.0.0 4.3.5 0.1.0

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    post your code that you have worked on so far
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Sep 2012
    Quote Originally Posted by rogster001 View Post
    post your code that you have worked on so far
        FILE *file;
        int c;
        char **file_buffer;
        file_buffer = (char**)malloc(9*sizeof(char));
        for(int x =0;x<9;x++){
                file_buffer[x] = (char*)malloc(1*sizeof(char));
        file = fopen("C:\\Users\\S2xC\\Documents\\sudoku.txt","r");
        int x=0;
        int y=0;
                       c = fgetc(file);
                                  file_buffer[x][y] = (char)c;
                                  c = fgetc(file);
    Last edited by 'Serj Codito; 09-08-2012 at 03:55 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > file_buffer = (char**)malloc(9*sizeof(char));
    A couple of points.
    1. Don't cast the result of malloc -> FAQ
    2. You got the size wrong, it should be sizeof(char*)

    Generally, if you write
    file_buffer = malloc(9*sizeof(*filebuffer));
    you're always going to be right.
    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.

  5. #5
    Join Date
    Dec 2007

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    FYI, this is C, not C++. Next time, please post it in the C forum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About char pointer to char array
    By homoon in forum C Programming
    Replies: 3
    Last Post: 05-27-2012, 03:26 AM
  2. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  3. Two dimensional char array
    By eurus in forum C Programming
    Replies: 17
    Last Post: 01-19-2006, 12:06 AM
  4. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM
  5. Char-array vs Char-pointer
    By ripper079 in forum C++ Programming
    Replies: 12
    Last Post: 09-09-2002, 01:16 AM