Trying to put numbers from a txt file into an aray, without a huge amount of luck!

This is a discussion on Trying to put numbers from a txt file into an aray, without a huge amount of luck! within the C Programming forums, part of the General Programming Boards category; Hey guys, just signed up as I'm pretty sure you can help me with a few things! I'm pretty new ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    17

    Trying to put numbers from a txt file into an aray, without a huge amount of luck!

    Hey guys,
    just signed up as I'm pretty sure you can help me with a few things!
    I'm pretty new to C, so my code will probably be ridden with noob errors... But... Here it goes!

    I'm trying to write a program which reads a set of numbers from a txt file and puts them into an array; trouble is I keep having trouble with EOF errors, which loop type to use etc.

    The numbers are stored in the txt file like this:
    99
    1890
    777
    6611

    So, a new number each line, and only integer numbers.

    My code so far is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define TXTFILE "numbers.txt"
    FILE *FPTR;                 //File Pointer
    int numint;                 //Integer variable to store File contents
    int numbarr[100];           //Array to store file contents
    int i=0;                    //Iteration counter
    int n;                 //Integer variable to contain the number of values in the file
    
    main(void)
    {
          printf("Number Analysis Tool\n");
          FPTR = fopen(TXTFILE,"r"); 
          if(!FPTR) 
          {
                       printf("File is missing, empty or corrupted.\n");
          } 
          else
          {
              printf("File opened successfully.\n");
          }
          while((numint = fgetc(FPTR)) != EOF)
          {     
               i++;
          }
          n=i;
          printf("Number of digits within the txt file: %d\n", n);
          getchar();
          int i=0;
          while((numint = fgetc(FPTR)) != EOF && i>=0 && i<=n)
          {
                   numbarr[i]=numint;
                   putchar(numbarr[i]);
                   i++;
          }
          fclose(FPTR); 
          getchar(); 
          
    }
    It seems to find the number of digits in the txt file ok, which is the variable n, but further than that it doesn't do much. It also compiles completely fine.

    Any help is much appreciated!
    Cheers,
    Will

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well to read a file for a second time, having hit EOF, you need to do either

    rewind(FPTR);

    or
    fseek(FPTR,0,SEEK_SET);
    clearerr(FPTR);



    Some other points.
    1. Variable names should be in lowercase or mixedCase. All UPPERCASE names are generally taken to be pre-processor #defines.
    2. Move all your variables inside main() - you should get out of the habit of casual global variables.
    3. Make main explicitly return an int, and have a return 0; at the end
    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.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Are you sure you don't want to end up with something like this?
    numarr[0] = 99
    numarr[1] = 1890
    numarr[2] = 777
    numarr[3] = 6611

    The way you're going about it now seems to be the slowest way.
    FAQ > How do I get a number from the user (C) - Cprogramming.com

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    17
    Quote Originally Posted by whiteflags View Post
    Are you sure you don't want to end up with something like this?
    numarr[0] = 99
    numarr[1] = 1890
    numarr[2] = 777
    numarr[3] = 6611
    Pretty much, I guess that's an example of a multidimensional array? I'll read up on them, got the big "C programming Language" next to me.

    And cheers Salem, those commands should help a hell of a lot!

    And wow, quick responses guys! Thx. I went into the garden to do some exercise, came back and POW! Answers! :P

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    No, that's not a multidimensional array. This is a good link to learn arrays.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    17
    Got it working!! Cheers guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read large amount of numbers from file
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2007, 09:33 AM
  2. file i/o and plain old bad luck
    By rokenrol in forum C Programming
    Replies: 7
    Last Post: 07-09-2006, 02:16 PM
  3. Using huge numbers
    By maxorator in forum C++ Programming
    Replies: 5
    Last Post: 10-11-2005, 09:56 AM
  4. Huge Numbers in C++
    By jdm in forum C++ Programming
    Replies: 13
    Last Post: 04-21-2004, 05:17 AM
  5. how unlimited the amount of numbers to average
    By Machewy in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2003, 08:44 AM

Tags for this Thread


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