Fill array with data from file

import data from file into an array and search array

Page 1 of 1

4 Replies - 12970 Views - Last Post: 12 November 2008 - 08:18 AM Rate Topic: -----

#1 dark_pun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-November 08

Fill array with data from file

Posted 11 November 2008 - 11:44 PM

I have a file called 'data.txt' with a list of numbers I need imported into an array. and from there, prompt the user for an integer to search for in the array, and display whether or not that integer is in the array in addition to its location. The only problem I am having is with importing the data from my file into the array. Here is my code.

#include <stdio.h>		  

#define NOT_FOUND -1

int search( const int arr[], int target, int n);
int main(void)
{
	int x[13];
	int index;
	int target;
FILE* data_in;
data_in=fopen("data.txt", "r");
fscanf(data_in , "%d" , &x[13] );
printf("%d\n", x[1]); //test to see if scan from data.txt is successful

printf("Please enter a value (-1 = done)> ");
scanf("%d", &target);

	index = search( x, target, 10);

	if (index == NOT_FOUND )
		printf("%d is not in the array.\n", target);
	else
		printf("%d is located at position %d in the array.\n", target, index);
	return(0);
} 



My search function works fine. Any ideas on how to scan that file data into my array would be appreciated. I'd like my output to look like...

$ ./binary.out
Array before sort = 12 7 8 17 2 20 19 16 14 3 2 1
Array after sort = 1 2 2 3 7 8 12 14 16 17 19 20

Please enter a value (-1 = done)> 3
3 is located at position 3 in the array.

Please enter a value (-1 = done)> 14
14 is located at position 7 in the array.

Is This A Good Question/Topic? 0
  • +

Replies To: Fill array with data from file

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Fill array with data from file

Posted 12 November 2008 - 12:21 AM

use the cpp way of handling files...froget C.

here's a starter


  int nRead = 0

  ifstream f;

  f.open("nums.txt");

  if (f)
  {
	  while (f >> nRead ) 
	   {
		  //you have the number in nRead
	   }

	f.close();
  }




Was This Post Helpful? 0
  • +
  • -

#3 Linkowiezi  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: Fill array with data from file

Posted 12 November 2008 - 12:47 AM

Or, if you wanna use C you can put this in your code:
for( int i = 0; i < 13; i++ )
fscanf(data_in , "%d" , &x[i] );
fclose (data_in);


If you have 13 numbers in your 'data.txt' file that is but my guess is that you do since you declared your array x[13].
Was This Post Helpful? 1
  • +
  • -

#4 dark_pun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-November 08

Re: Fill array with data from file

Posted 12 November 2008 - 01:27 AM

Thanks! I'd rather use C because I am not yet familiar with C++.
Next step: The program imports the data into my array, and prints it successfully. Now, since I want it to be able to search for an integer in the array, I need user input for the target variable. this is what I have, including the search function:

#include <stdio.h>		  

#define NOT_FOUND -1

int search( const int arr[], int target, int n);
int main(void)
{
	int x[12];
	int index;
	int target, m;

FILE* data_in;
data_in=fopen("data.txt", "r");

	for (m=0; m<12; ++m){
	fscanf(data_in , "%d" , &x[m] );
	}
fclose (data_in);

//tried scanf("%d", &target); here 

printf("Array before sort =  %d %d %d %d %d %d %d %d %d %d %d %d\n", x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11]);


printf("Please enter a value (-1 = done)> ");
	
		index = search(x, target, 12);

		if (index == NOT_FOUND ) 
		printf("%d is not in the array.\n\n", target);
		else
		printf("%d is located at position %d in the array.\n\n", target, index);
	

	return(0);
} 

int search( const int arr[],	   /* input - array to search */
int target,	   /* input - value searched for */
int n)	  /* input - number of elements to search */
{


	int i, /* loop control */
		found=0, /* flag whether or not target was found */
		where; /* index where target found or NOT_FOUND */


//tried scanf("%d", &target); here also


	/* Compare each element to target */
	while ( !found && i<n )
	{
	   if ( arr[i] == target )
		  found = 1;
	   else
		 ++i;
	}

	/* Return index of matching element or NOT_FOUND */
	if ( found )
		 where = i;
	  else
		 where = NOT_FOUND;

	return(where); 
} 



I already tried putting a
 scanf("%d", &target); 
where I specified in a comment in the code above..But I'm getting segmentation faults. any ideas?
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Fill array with data from file

Posted 12 November 2008 - 08:18 AM

Quote

...I already tried putting a
 scanf("%d", &target); 
where I specified in a comment in the code above..But I'm getting segmentation faults. any ideas?


See this ... :D
#include <stdio.h> 
#include <stdlib.h>         

#define NOT_FOUND -1
#define MAX 12

int search( const int arr[], int target, int n );
void showAry( int arr[], int n );


int main()
{
    int x[MAX];
    int index;
    int c, target, m;
    
    FILE* fin;
    fin=fopen("data.txt", "r");
    if(fin==NULL) 
    {
        printf("Error opening file ... Press 'Enter' to exit ... ");
        getchar();
        return -1;
    }
    /* m holds the number elements when loop has reached EOF */
    for (m = 0; fscanf(fin, "%d" , &x[m]) != EOF ; ++m) {;} 
    fclose (fin);

    showAry( x, m ); 
    
    printf( "\n\nEnter a 'target' to search for in the above list : ");
    scanf(" %d", &target);
    while( (c=getchar())!='\n' ) c=getchar();
    
    index = search(x, target, MAX);
    if (index == NOT_FOUND )
        printf("%d is not in the list.\n\n", target);
    else
        printf("%d is located at INDEX %d in the list.\n\n", target, index);
    
    getchar();
    return(0);
}

void showAry( int arr[], int n )
{
    int i;
    for( i=0; i<n; ++i )
        printf( "%d ", arr[i] );
}

int search( const int arr[], int target, int n )
{
    int i;
    for( i=0; i<n; ++i )
        if( target==arr[i] ) return i;
        
    return -1;
} 

/* data.txt */

/*
5
10
20
4
11
12
10
7
*/


This post has been edited by David W: 12 November 2008 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1