Thread: Inputing a file to an array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Inputing a file to an array

    I made a simple program to input a text file into an array and then have have the user search for any number in the array. The search function works fine, I have tested it multiple times. But for some reason I cant get the text file to input into the array. Here is the code in the main c file:

    Code:
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include "search.h"
    
    #include "filereader.h"
    
    
    int main ()
    
    {
    
      FILE *ifp;
    
      ifp = fopen("test.txt", "r");
    
    
      int index, size, number, x, i;
    
    
      printf("What number do you want to find?: ");
    
      scanf("%d",&number);
    
    
      size = 0;
    
      i = 0;
    
      while ((fscanf(ifp, "%d", &x)) == 1)
    
        {
    
          size++;
    
          i++;
    
        }
    
      
    
      int *num = malloc(size * sizeof(int));
    
      FileReader(ifp, num);
    
      
    
      for(i = 0; i < size; i++)
    
        printf("%d", num[i]);
    
    
      index = Search(num, size, number);
    
      fclose(ifp);
    
    
      if(index != -1)
    
        printf("Found it in index: %d\n", index);
    
      else
    
        printf("Didn't find it\n");
    
    
    }


    and here is the code for the file reader function:

    Code:
    
    #include <stdlib.h>
    
     
    #include "filereader.h"
    
    
     
    void FileReader(FILE *fp, int *numArray)
    
     
    {
    
     
      int i, x;
    
     
      i = 0;
    
     
      while ((fscanf(fp, "%d", &x)) == 1)
    
     
        {
    
     
          numArray[i] = x;
    
     
          i++;
    
     
        }
    
     
    }


    When i put the code that is in the FileReder function in main it works perfectly but when I try to use an outside function to read in the file it doesn't work. I ouput the array in main to see what it stored in it and it's always all 0's. Please help

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    By the time you call your file reader function you're already at the end of the file.... OOPs, nothing to read.

    Also don't be double and triple line spacing... it does not make your code any easier to read.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Thank you! I just changed how I found the size of the file. But is there a way to go back to the beginning of the file besides closing it and re-opening it?
    Sorry about the spacing it did that when I copied it over

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by m-kosier View Post
    But is there a way to go back to the beginning of the file besides closing it and re-opening it?
    fseek - C++ Reference

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by m-kosier View Post
    Thank you! I just changed how I found the size of the file. But is there a way to go back to the beginning of the file besides closing it and re-opening it?
    Sorry about the spacing it did that when I copied it over
    No worries about spacing... just a reminder.

    To get back to the beginning of the file you can use
    Code:
    fseek(file, 0, SEEK_SET);  // set pointer in file 0 bytes from origin
    
    //or
    
    rewind(file);   // rewind to beginning

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2002, 07:08 PM
  2. Inputing strings into a dynamic array
    By Nakeerb in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2002, 02:38 AM
  3. scanning in with gets() but not over inputing
    By stephanos in forum C Programming
    Replies: 1
    Last Post: 09-09-2002, 03:38 PM
  4. inputing from file to multi-dimensional array
    By jmedia420 in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 06:14 PM
  5. Need Help Inputing from a data file
    By sujr21 in forum C Programming
    Replies: 5
    Last Post: 12-05-2001, 05:20 PM