-1

I'm starting to learn C by myself. I've been trying to learn as much of the other languages myself also (such as Java, HTML, CSS). Anyways I have a basic question regarding inserting a text file of numbers into an int array.

This is how I'm starting it (let me know if I'm doing anything wrong).

  1. I created a text file using the Linux command prompt: cat > input.txt

  2. I entered random numbers of my choosing into input.txt: 14 21 78 14

  3. I want to load the numbers from input.txt somehow into my array that I made in my arrayTest.c file.

Here is the array that I made:

main() {
int num[100]; // My array that I want to insert numbers from input.txt into
}

void readNumbers() { // Function that reads the numbers
/* Not really sure how to start this -- help would be nice */
}

void displayNumbers() { // Function that displays the numbers in the output
printf("Your numbers are:\n", num[1], num[2], num[3], num[4]); // Just an example

And then the output would display as so:

Your numbers are: 14 21 78 14

I've been looking at tutorials and I just can't seem to get it.

  • 1
    There is no problem in learning C by yourself, like me, but do not only learn from online tutorials, as they do not cover everything properly. Choose a good C book, from [here](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), and use that as a reference for yourself. – Box Box Box Box Feb 08 '16 at 05:55
  • 1
    It doesn't seem like there's a specific C question here, but more like a general programming question about how to organize your code and how to read numbers from a text file. – Tom Karzes Feb 08 '16 at 05:57
  • Arrays are zero based, just as a heads up. Look up the fopen function, and start smaller. Just try opening a file and then reading after successfully opening it. – Christopher Schneider Feb 08 '16 at 06:01
  • How do you open a file though? That's my question. – Jordan Bucht Feb 08 '16 at 06:08
  • @JordanBucht You have to use fOpen() function for opening a file – Jaffer Wilson Feb 08 '16 at 06:11

2 Answers2

1

Look at the documentation for file opertions; fopen, fread, and fclose. These let your code access the contents of a file.

Once you have read the data, or as part of reading, you can get the individual numbers.

You can learn by breaking the task down into smaller parts, for example, pretend that you have already read the file into a string and just work on the part to get the numbers out of that.

john elemans
  • 2,578
  • 2
  • 15
  • 26
0

You can read in C like this

#include "stdio.h"
#include "stdlib.h"

// declaring globally to access in all functions (or pass as a parameter)
int num[100];
int numIterator = 0;

void readNumbers() {  // function that reads the numbers
    int num[100];

    FILE *fp1;
    char oneword[100];
    char c;

    fp1 = fopen("input.txt", "r");

    do {
        c = fscanf(fp1, "%s", oneword);
        num[numIterator++] = atoi(oneword);
    } while (c != EOF);

    fclose(fp1);
}

void displayNumbers() {  // function that displays the numbers in the output
    int t = numIterator;
    printf("Your numbers are: ");
    do {
        printf("%d ", num[t--]);
    } while (t >= 0);
}

main() {
    // call readNumbers() here first
    // call displayNumbers() here after reading numbers from file
}

Ps. @barak manos is correct about scope of num array you are declaring.

Fahad Siddiqui
  • 1,829
  • 1
  • 19
  • 41