Thread: Simple array help needed

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Simple array help needed

    I have a text file with spaces and new lines, how do i read it into a character array such that to maintain the formatting?
    Code:
    int main(){
        char* ar = readScores();
    
    }
    
    char* readScores() {
    
        char* ar = (char*)malloc((40)*sizeof(char));
        for(int i=0; i<40; i++){
            scanf("%s", ar[i]);
            printf("%s", ar[i]);
        }
        return ar;
    }
    Last edited by johngoodman; 02-12-2013 at 07:52 PM.

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Here's another thought I had :
    Code:
        int i = 0;
        char* stackArray[100];
        //char* ar = (char*)malloc(*count)*sizeof(char));
        do    {
            scanf("%s", &stackArray[i]);
            printf("%s", stackArray[i]);
            i++;
        } while(*stackArray[i] != NULL);
    then copy the stackArray into an array on the heap while allocating memory for it...doesn't work, but the idea is there...just not sure how to put it into code

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    What is the format of your text file? This will determine how you will parse it. There should be some data in the file, a file with nothing but whitespace is pretty useless.

    Code:
    int main(){
        char* ar = readScores();
    
    }
    
    char* readScores() {
    
        char* ar = (char*)malloc((40)*sizeof(char));
        for(int i=0; i<40; i++){
            scanf("%s", ar[i]);
            printf("%s", ar[i]);
        }
        return ar;
    }
    This code should and probably will, crash. You have 40 bytes allocated, then you try to read an entire string "%s" into a single byte. Not only that, the expression ar[i] is a character value, not a pointer to character. If you want a pointer to char, use ar + i, or even &ar[i]. In any case, you need to allocate enough space for how ever many characters you are reading. With what you have now, you can read at most 39 characters at one time into the ar buffer.

    Read the C tutorials on this site to learn how to read lines from a file, or search for "man fgets" and "man fscanf" to parse data. Without a description of your file format, that's about all I can say.
    Last edited by MacNilly; 02-12-2013 at 08:36 PM.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by MacNilly View Post
    What is the format of your text file? This will determine how you will parse it. There should be some data in the file, a file with nothing but whitespace is pretty useless.

    Code:
    int main(){
        char* ar = readScores();
    
    }
    
    char* readScores() {
    
        char* ar = (char*)malloc((40)*sizeof(char));
        for(int i=0; i<40; i++){
            scanf("%s", ar[i]);
            printf("%s", ar[i]);
        }
        return ar;
    }
    This code should and probably will, crash. You have 40 bytes allocated, then you try to read an entire string "%s" into a single byte. Not only that, the expression ar[i] is a character value, not a pointer to character. If you want a pointer to char, use ar + i, or even &ar[i]. In any case, you need to allocate enough space for how ever many characters you are reading. With what you have now, you can read at most 39 characters at one time into the ar buffer.

    Read the C tutorials on this site to learn how to read lines from a file, or search for "man fgets" and "man fscanf" to parse data. Without a description of your file format, that's about all I can say.




    This is the test text file:

    this is a test
    this is only a test
    this is also a test

    I just want to allocate memory and store each character in an array on the allocated memory

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    You could allocate a buffer for each line, then copy the line from the file into the corresponding buffer. Or, you could create one buffer that stores the contents of the file. I'll assume you want to do the former. The problem is that you can't know in advance how many lines will be in the file. But, for simplicity, assume that there are at most N lines in the file and that each line is at most M characters long, including the newline. Then the code

    Code:
        char** lines = malloc(N * M);
        int i = 0;
    
        while (i < N && fgets(lines[i], M, stdin))
            ++i;
    reads up to N lines from standard input or until an error or EOF occurs. The variable i will be how many lines were read successfully. Note the use of char** instead of just char*. The reason is that "lines" is used as a 2-dimensional array, the first dimension being the lines themselves, the second dimension being the characters in each line. If you just used char*, when you dereferenced the pointer you'd get a char, not a char pointer, which you must have in order to pass to fgets.
    Last edited by MacNilly; 02-12-2013 at 08:58 PM.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Apologies, the previous code was incorrect. I actually tested the code below, it seems to work. Hope this helps you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    #define N 10
    #define M 80
    
    int main()
    {
            char* lines = malloc(N * M);
            int num_lines = 0;
            int i;
    
        assert(lines);
    
            while (num_lines < N && fgets(lines + num_lines * M, M, stdin))
                    ++num_lines;
    
            printf("read %d lines\n", num_lines);
    
            for (i = 0; i < num_lines; ++i)
                    printf("%s", lines + i * M);
    
            return 0;
    }

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It's bad to use assert like that.

    The point of assert is that you check your assumptions, like you didn't forget to initialise a pointer, one variable never goes over a certain value, ect...

    When it comes to the final code, you define NDEBUG and all those asserts disappear.

    So, the problem with asserting that malloc didn't fail is that is something that is determined at runtime. When you define NDEBUG (to make your program run nice and fast) that assert will disappear, and if malloc fails, your program will segfault.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    1
    can anyone please tell me the complete procedure of getting connected with oracle database in C

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, abhishek99!

    You need to stop posting in this thread, and ask your question in a new thread. It won't get the attention it deserves here, and it dilutes the thread, as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed with simple code
    By Branka in forum C++ Programming
    Replies: 6
    Last Post: 08-18-2005, 03:00 AM
  2. help needed with simple code
    By Branka in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2005, 10:25 AM
  3. Help needed. Simple C++ program.
    By Sridar in forum C++ Programming
    Replies: 20
    Last Post: 01-28-2005, 06:57 PM
  4. Tic Tac Toe Simple Game Needed!
    By Ruski in forum C++ Programming
    Replies: 13
    Last Post: 06-28-2002, 07:18 AM
  5. Simple AI Needed!
    By minime6696 in forum Game Programming
    Replies: 2
    Last Post: 02-15-2002, 09:38 PM