Read char by char into two-dimensional array pointer.

This is a discussion on Read char by char into two-dimensional array pointer. within the C++ Programming forums, part of the General Programming Boards category; Hello guys could you please help me write a piece of code that will read a file char by char ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    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
    Location
    Liverpool UK
    Posts
    1,422
    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."

    Stroustrup:
    "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
    Posts
    2
    Quote Originally Posted by rogster001 View Post
    post your code that you have worked on so far
    Code:
        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;
        if(file!=NULL){
                       c = fgetc(file);
                       while(c>=0){
                                  file_buffer[x][y] = (char)c;
                                  c = fgetc(file);
                                  y++;
                                  if(c=='\n'){
                                              x++;
                                              y=0;
                                              }
                                               
                                   }
                       }
    Last edited by 'Serj Codito; 09-08-2012 at 03:55 AM.

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

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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-18-2006, 11:06 PM
  4. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 05:18 PM
  5. Char-array vs Char-pointer
    By ripper079 in forum C++ Programming
    Replies: 12
    Last Post: 09-09-2002, 01:16 AM

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