how to store the file data into an array in c++

Posted on 2007-10-17
Last Modified: 2008-01-09
i m having a file containing the numbers as 12 3 4 67 and i want to store them in an array...
Question by:trivenibomma
    LVL 45

    Expert Comment

    Hi trivenibomma,

    You'll have to read them from the file and store them.  C++ has quite a few ways to do this.

    Good Luck,
    LVL 15

    Expert Comment

    Try something like this:
    #include <stdio.h>
    #include <string.h>

    #define TABSIZE 10  /* Number of entries in array */
    #define MAX_LEN 256 /* Maximum buffer size */

    main() {
       FILE *f;                  /* Input file */
       int  tab[TABSIZE];        /* Table for numbers */
       char buffer[MAX_LEN + 1]; /* One extra byte needed for NULL */
       int  count;               /* Number of numbers picked up from file */

       f = fopen("xx.txt", "r"); /* Open the file */
       while (fgets(buffer, MAX_LEN, f)) { /* Read a line */
          int iX = 0;
          char *pStr = strtok(buffer, " ");
          while (pStr && iX<TABSIZE) {
             tab[iX++] = strtol(pStr, NULL, 10);
             pStr = strtok(NULL, " ");
          if (pStr) { /* Is the table full? */
             fprintf(stderr, "The table size of %d is too small\n", TABSIZE);
          count = iX;
       /* Now we have "count" entries in "tab" */
       printf("Found %d numbers\n", count);

       fclose(f); /* Tidy up - close the file */
    LVL 53

    Accepted Solution

    If this is homework, then please tell us so, so that we can help you as good as possible.

    Take a look at this tutorial on input/output with files :


    Give it a try, and post what you have here ...
    LVL 28

    Assisted Solution

    If the size of the array (the number of elements) is not known, then probably std::vector is better choice for storing the elements. It may even be better also when you know the number of elements.

    You did not say if the file contains also other things than the numbers and how the numbers are separated. I guess it is a text file, is it?

    The bpmurray's solution is a C solution. The clean C++ solution would look a bit differently. As Infinity08 said, you should present your own attempt and ask more detailed questions.

    Author Comment

    ok i got it....
    LVL 53

    Expert Comment

    Does it work ? If you show your code, we can have a look at it to see if it's ok.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    how to understand recursion 12 192
    Problem to show menu 5 70
    Problem to scan all sheets 3 65
    Dynamically allocate memory 9 41
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now