Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2007-10-17
Medium Priority
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 46

Expert Comment

by:Kent Olsen
ID: 20096176
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

ID: 20096465
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

Infinity08 earned 240 total points
ID: 20096512
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 ...
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 29

Assisted Solution

pepr earned 135 total points
ID: 20096747
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

ID: 20096982
ok i got it....
LVL 53

Expert Comment

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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

569 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