Help with File IO and arrays

Trying to get a file into an array before putting it into a linkedlist

Page 1 of 1

5 Replies - 563 Views - Last Post: 31 March 2010 - 11:52 PM Rate Topic: -----

#1 Guest_Ford*


Reputation:

Help with File IO and arrays

Posted 31 March 2010 - 05:45 PM

I need to write a program to open a file and copy the file (which is numbers 1 to 100 one per line) and save the numbers in a linked list. I thought the easiest way would be to save the numbers in an array then move them into a linked list (if there's a better way I'm open to it). I used gets to try and move the values into the array but I can't get it to work

Thanks for the help

#include <stdio.h>
#include <stdlib.h>
#define INPUT_FILE1 "C:\\Users\\Ford\\Documents\\Visual Studio 2008\\scores1.txt"

int main(void)
{
int scores1 [300];

FILE *fp;
/*output file to program*/
fp=fopen(INPUT_FILE1, "r");
if(fp==NULL)
{
	printf("Error cannot open file.\n");
	exit(1);
}
else
{
	printf("File opened successfully\n");
}
do
	{
	int i=0;
	gets(scores1[i+1]);
	i++;
	printf("%d",scores1[1]);
	getchar();
	}

while (fp!=EOF);


}



Is This A Good Question/Topic? 0

Replies To: Help with File IO and arrays

#2 Mallstrop  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 50
  • Joined: 19-June 08

Re: Help with File IO and arrays

Posted 31 March 2010 - 05:51 PM

Is there anything stopping you putting them straight into a linked list?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Ford*


Reputation:

Re: Help with File IO and arrays

Posted 31 March 2010 - 05:57 PM

View PostMallstrop, on 31 March 2010 - 04:51 PM, said:

Is there anything stopping you putting them straight into a linked list?



No I'm just new to C (freshman in college first semester programming) and it seemed that going from file to array to list would be easier as later I must compute the mean median and mode but if going straight to linked list can be done easier that's fine but I'm not exactly sure how to do that either
Was This Post Helpful? 0

#4 Mallstrop  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 50
  • Joined: 19-June 08

Re: Help with File IO and arrays

Posted 31 March 2010 - 06:08 PM

As a beginner I'd imagine that you're task is to implement your own linked list?

Learning how a linked list works and how to manipulate the data in one is all part of the fun. It's all quite simple when you get your head around it

I won't go into how to implement a linked list but every linked list should have a function to insert elements. This function makes linked lists even easier to use than an array, you don't even have to decide where you want the data to go, just tell it what you want to insert.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Help with File IO and arrays

Posted 31 March 2010 - 06:16 PM

just stream the file straight into the linked list, dont waste time with inputting it into an array first. A link list is just simply a structure that has a pointer of itself within itself(if that makes sense). You can then allocate that pointer with malloc() for a new index in your list.

create a loop
read in file line by line
if the new line is not end of file allocate a new slot in the linked list
set the number from the file to the value in the current index of the list
If you hit the end of the file break the loop
Was This Post Helpful? 1
  • +
  • -

#6 Guest_Ford*


Reputation:

Re: Help with File IO and arrays

Posted 31 March 2010 - 11:52 PM

View PostMallstrop, on 31 March 2010 - 05:08 PM, said:

As a beginner I'd imagine that you're task is to implement your own linked list?

Well my program has to open 3 files put them in three linked lists sort, calculate mean median and mode for all 3 files and then a merged copy and then output all results so implementing a linked list is only part of it I was just kinda stuck on it

Thanks ImaSexy I'll see if I can't get that working after I've gotten some sleep
oh quick side question: should I try to sort as I put the file into a linked list or after its already in a linked list?
Was This Post Helpful? 0

Page 1 of 1