soy nuevo aqui,y estoy aprendiendo C,y necesito resolver el siguiente problema con C,ya intente de varias formas y no estoy seguro que me piden,hasta ahora use fopen ,fscanf y alguna otra funcion,pero claro me funciona mi codigo si hago el mismo aparte, sin usar esas funciones que me dan,entonces quiero entender bien donde usar estos parametros de la funcion array_from_file :(int array[], unsigned int max_size,const char *filepath),lo que supongo que en la funcion array_from_file es para el primer ejercicio y la funcion array_dump es para el segundo por los argumentos que tienen.
EXISTE UN DIRECTORIO LLAMADO input y dentro de el muchos archivos,con nombres diferentes ,y en cada archivo el array se representa con su size y luego cada uno de los miembros.
por ejemplo el array = [1,2,3,4,5] es representado en el archivo como:
5 1 2 3 4 5
EJERCICIO 1: Se pide que tu programa principal, lea el array dentro de cada uno de los archivos dentro de input y que luego de leerlo imprima por pantalla su contenido de la siguiente forma:
[ 1, 2, 3, 4, 5]
El codigo lo debo ingresar donde dice TU CODIGO AQUI:
este es el main.c,donde debo agregar codigo :
/* First, the standard lib includes, alphabetically ordered */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
/* Maximum allowed length of the array */
#define MAX_SIZE 100000
void print_help(char *program_name) {
/* Print the usage help of this program. */
printf("Usage: %s <input file path>\n\n"
"Sort an array given in a file in disk.\n"
"\n"
"The input file must have the following format:\n"
" * The first line must contain only a positive integer,"
" which is the length of the array.\n"
" * The second line must contain the members of the array"
" separated by one or more spaces. Each member must be an integer."
"\n\n"
"In other words, the file format is:\n"
"<amount of array elements>\n"
"<array elem 1> <array elem 2> ... <array elem N>\n\n",
program_name);
}
char *parse_filepath(int argc, char *argv[]) {
/* Parse the filepath given by command line argument. */
char *result = NULL;
if (argc < 2) {
print_help(argv[0]);
exit(EXIT_FAILURE);
}
result = argv[1];
return (result);
}
unsigned int array_from_file(int array[],
unsigned int max_size,
const char *filepath) {
//TU CODIGO AQUI!!!
}
void array_dump(int a[], unsigned int length) {
//TU CODIGO AQUI!!!
}
int main(int argc, char *argv[]) {
char *filepath = NULL;
/* parse the filepath given in command line arguments */
filepath = parse_filepath(argc, argv);
/* create an array of MAX_SIZE elements */
int array[MAX_SIZE];
/* parse the file to fill the array and obtain the actual length */
unsigned int length = array_from_file(array, MAX_SIZE, filepath);
/*dumping the array*/
array_dump(array, length);
return (EXIT_SUCCESS);
}
para compilar usa: gcc -Wall -Werror -Wextra -pedantic -std=c99 -o reader main.c
para correr el programa: ./reader input/archivo
donde archivo es alguno de los archivos que se encuentra dentro de input.
Cambiar el archivo input y ejecutar nuevamente
EJERCICIO 2: Modificar main.c para que en lugar de leer el archivo input, lea cada uno de los miembros del array por pantalla y luego los muestre en la misma.