NEWS: Total Picture Views: 151536 | Total Article Views: 161026 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. PickUpThings with Becker Robot | 3. How to read a JSON and work with it | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

Loading File And Printing the Result Using Arrays C

Description:

In this example I will be showing how to load a file and printing the result in the console using arrays.

Code:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

typedef struct{
	char FirstName[30];
	char LastName[30];
	char Nationality[30];
	float Result;
} Athletes;


void printAtheletes(Athletes a[],double result){
	for (int i = 1; i < 42; i++){
		if (a[i].Result >= result){
			printf("%s \t %s \t %s \t %d \n", a[i].FirstName,a[i].LastName,a[i].Nationality[i],a[i].Result);
		}
	}
}

bool loadAtheletes(char fileName[10], Athletes myAthletes[]){
	char tempFirstName[30];
	char tempLastName[30];
	char tempNationality[30];
	float tempResult;
	int counter;

	FILE *input = fopen(fileName, "r+");

	//check if the file opened succesfully
	if (input == NULL){
		//perror("Error opening file");
		return false;
	} else {
		counter = 0;
		// continue in reading the file til the EOF
		while (!feof(input)){
			strcpy(tempFirstName, " ");
			fscanf(input, "%s %s %s %d", tempFirstName, tempLastName, tempNationality,&tempResult);
			if (strcmp(tempFirstName, " ") != 0){
				strcpy(myAthletes[counter].FirstName, tempFirstName);
				strcpy(myAthletes[counter].LastName, tempLastName);
				strcpy(myAthletes[counter].Nationality, tempNationality);
				myAthletes[counter].Result = tempResult;
				counter++;
			}
		}
		fclose(input);
		return true;		
	}
}

int main(int argc, char *argv[])
{
	Athletes myAtheletes[41];
	double str1;
	char myFileName[10] = "cust.txt";
	if (loadAthletes(myFileName,myAtheletes)){
		printf("The data are loaded successfully.");
		printf("Entrer a distance:\n");
		scanf("%d", &str1);
		getchar(); //Clean the buffer
		for (int i = 1; i < 42; i++){
			printAthletes(myAtheletes[i],str1);
		}
	}
	else{
		printf("We could not open the file.");
	}
	system("pause");
	return 0;
}
Attachments: None

Tags: Arrays Load File C

Total Views: 765

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 02/25/15


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.