In the function imprimir I have a problem when I do the fprintf in a file, because _materias->nombres does not return the value, for this reason, the text not show this information, please help because i don't know what is happening, if other person know how to create the in the struct nombre in array is better, because i don't do this for reason of the strtok because he return a char pointer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *nombre;
int creditos;
float nota;
} curso;
int conteo (FILE * _entrada);
void semestre (FILE * _entrada, curso * _materias, int *_cantidad,
int *_ganadas, int *_perdidas, float *_promedio);
void imprimir (curso * _materias, int *_cantidad, int *_ganadas,
int *_perdidas, float *_promedio);
int main (int argc, char *argv[])
{
int ganadas = 0;
int perdidas = 0;
float promedio = 0.0;
int cantidad = 0;
char archivoEntrada[256];
curso *materias;
printf ("Archivo de entrada \n");
scanf ("%255s", archivoEntrada);
FILE *entrada;
entrada = fopen (archivoEntrada, "r");
if (entrada == NULL) {
printf ("No se logro abrir el archivo de entrada\n");
exit (EXIT_FAILURE);
}
cantidad = conteo (entrada);
materias = (curso *) malloc (sizeof (curso) * cantidad);
semestre (entrada, materias, &cantidad, &ganadas, &perdidas, &promedio);
imprimir (materias, &cantidad, &ganadas, &perdidas, &promedio);
free (materias);
}
int conteo (FILE * _entrada)
{
int i = 0;
char auxiliar[40];
while (!feof (_entrada)) {
fgets (auxiliar, 40, _entrada);
i++;
}
rewind (_entrada);
return i / 3;
}
void semestre (FILE * _entrada, curso * _materias, int *_cantidad,
int *_ganadas, int *_perdidas, float *_promedio)
{
int i = 0;
int sumaCreditos = 0;
float sumaNotas = 0.0;
char auxiliar2[100];
char *token;
fgets (auxiliar2, 100, _entrada);
while (i < *_cantidad) {
fgets (auxiliar2, 100, _entrada);
token = strtok (auxiliar2, ":");
token = strtok (NULL, ":");
(_materias->nombre) = token;
fgets (auxiliar2, 100, _entrada);
token = strtok (auxiliar2, ":");
float val2 = atof (strtok (NULL, ":"));
(_materias->nota) = val2;
fgets (auxiliar2, 100, _entrada);
token = strtok (auxiliar2, ":");
float val = atoi (strtok (NULL, ":"));
(_materias->creditos) = val;
sumaCreditos = sumaCreditos + (_materias->creditos);
if ((_materias->nota) < 3.0) {
*_perdidas = (*_perdidas) + 1;
}
else {
*_ganadas = (*_ganadas) + 1;
}
sumaNotas = sumaNotas + ((_materias->nota) * (_materias->creditos));
i++;
*_materias++;
}
*_promedio = (sumaNotas / sumaCreditos);
}
void imprimir (curso * _materias, int *_cantidad, int *_ganadas,
int *_perdidas, float *_promedio)
{
char archivoSalida[256];
FILE *salida;
printf ("Archivo de salida \n");
scanf ("%255s", archivoSalida);
salida = fopen (archivoSalida, "w");
if (salida == NULL) {
printf ("No se logro abrir el archivo de salida\n");
exit (EXIT_FAILURE);
}
int i = 0;
fprintf (salida, "Archivo de Salida: \n");
fprintf (salida, "Materia\tNota\tCreditos \n");
while (i < *_cantidad) {
fprintf (salida, "%s\t%f\t%d \n", (_materias->nombre),
(_materias->nota), (_materias->creditos));
*_materias++;
i++;
}
fprintf (salida, "\nTotal de materias: %d \n", *_cantidad);
fprintf (salida, "Materias ganadas: %d \n", *_ganadas);
fprintf (salida, "Materias perdidas: %d \n", *_perdidas);
fprintf (salida, "Promedio ponderado: %f \n", *_promedio);
}
Related
i have this function that extract the first name and the gendre form a file combined and then seperate them
for example the function extract "Aaliyah,F" and the separate them as prenom = Aaliyah and genre = F
the problem is that the function does not update the genre value and still at NULL
so for this code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int AleaNombre(int min, int max) {
return (rand() % (max - min + 1)) + min;
}
void recuperer_prenom(char genre , char prenom[20] ) {
FILE* f2 = fopen("C:\\Users\\PC PRO DZ\\Desktop\\Projects\\C\\TP essai\\prenom.txt", "r");
fseek(f2, 0, SEEK_END);
int length = ftell(f2);
fseek(f2, 0, SEEK_SET);
char line1[length + 1];
fgets(line1, length + 1, f2);
int i = AleaNombre(1, 100);
int cpt = 0;
char* tok = strtok(line1, " ; ");
cpt++;
while (cpt != i) {
tok = strtok(NULL, " ; ");
cpt++;
}
//separer le prenom et le genre tel que prenom,genre et le genre est le dernier caractere en utilisant strtok
char* tok2 = strtok(tok, ",");
strcpy(prenom, tok2);
tok2 = strtok(NULL, ",");
genre = tok2[0];
printf("%s %c", prenom, genre); // the result is for example: "Aaliyah F"
rewind(f2);// ^ ^
fclose(f2);// |prenom| |genre|
}
int main () {
srand (time(NULL));
char prenom[20];
char genre;
recuperer_prenom(genre, prenom);
printf ("%s %c", prenom, genre);// however the result here is "Aaliyah"
return 0;
}
i tried changing the way that the prenom and genre are separated and change the type of genre from char to a sting but didn'f fixe it
I have removed the random part and created a struct to store your fields:
You should pass by reference as said in comment
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAXLINE 256
typedef struct data {
char prenom[20];
char genre;
} data;
void recuperer_prenom(data *d) {
FILE* f2 = fopen("prenom.txt", "r");
char line1[MAXLINE];
fgets(line1, MAXLINE, f2);
char* tok = strtok(line1, " ; ");
//separer le prenom et le genre tel que prenom,genre et le genre est le dernier caractere en utilisan>
char* tok2 = strtok(tok, ",");
strcpy(d->prenom, tok2);
tok2 = strtok(NULL, ",");
d->genre = tok2[0];
printf("%s %c\n", d->prenom, d->genre); // the result is for example: "Aaliyah F"
fclose(f2);// |prenom| |genre|
}
int main () {
data d;
recuperer_prenom(&d);
printf ("%s %c\n", d.prenom, d.genre);// however the result here is "Aaliyah"
return 0;
}
I’m trying to make a program that receives a text from a file where its lines are a first name, a last name and a note. I want to return the ordered lines: first the students with a grade lower than 5 and after the students passed in order of appearance in the file. The problem is that I can’t print the names and I don’t know what the fault is. The main code is as follows:
int main(int argc, char *argv[])
{
char line[MaxLinea+1];
char *name;
char *surname;
lista_notas *mi_lista = NULL;
int i, blank, grade;
FILE *archivo = fopen(argv[1], "r");
while(fgets(line, MaxLinea, archivo)) //recorrer linea fich
{
grade = get_grade(line);
if (grade < 5) //Insertar en la lista
{
name = get_name(line);
surname = get_surname(line);
insertar(&mi_lista, name, surname, grade);
}
}
fclose(archivo);
archivo = fopen(argv[1], "r");
while(fgets(line, MaxLinea, archivo)) //recorrer linea fich
{
grade = get_grade(line);
if (grade > 4) //Insertar en la lista
{
name = get_name(line);
surname = get_surname(line);
insertar(&mi_lista, name, surname, grade);
}
}
mostrar_lista(mi_lista);
free_lista(mi_lista);
//system("leaks a.out");
return 0;
}
The get_name function is:
char *get_name(char *line)
{
int i;
char *name = malloc(51);
if (name == NULL)
exit(71);
i = 0;
while(line[i] != ' ')
name[i] = line[i++];
name[i] = 0;
return name;
}
If the program receives a fich.txt which content is:
Nombre1 Apellido1 8
Nombre2 Apellido2 2
Nombre3 Apellido3 6
The output must be:
Nombre2 Apellido2 2
Nombre1 Apellido1 8
Nombre3 Apellido3 6
but instead, it is:
Apellido2 2
Apellido1 8
Apellido3 6
Thanks for your help!
The full code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxLinea 154
//Tamaño máximo linea = 50 (nombre) + 100 (apellido) + 2 (nota máx 10)
// + 2 espacios + caracter nulo? = 155
typedef struct notas{
char *nombre;
char *apellido;
int nota;
struct notas *siguiente;
} lista_notas;
int get_grade(char *line);
char *get_name(char *line);
char *get_surname(char *line);
void insertar(lista_notas **ptr, char *name, char *surname, int grade);
void mostrar_lista(lista_notas *ptr);
void free_lista(lista_notas *ptr);
int main(int argc, char *argv[])
{
char line[MaxLinea+1];
char *name;
char *surname;
lista_notas *mi_lista = NULL;
int i, blank, grade;
FILE *archivo = fopen(argv[1], "r");
while(fgets(line, MaxLinea, archivo)) //recorrer linea fich
{
grade = get_grade(line);
if (grade < 5) //Insertar en la lista
{
name = get_name(line);
surname = get_surname(line);
insertar(&mi_lista, name, surname, grade);
}
}
fclose(archivo);
archivo = fopen(argv[1], "r");
while(fgets(line, MaxLinea, archivo)) //recorrer linea fich
{
grade = get_grade(line);
if (grade > 4) //Insertar en la lista
{
name = get_name(line);
surname = get_surname(line);
insertar(&mi_lista, name, surname, grade);
}
}
mostrar_lista(mi_lista);
free_lista(mi_lista);
//system("leaks a.out");
return 0;
}
void insertar(lista_notas **ptr, char *name, char *surname, int grade)
{
lista_notas *p1, *p2;
p1 = *ptr;
if(p1 == NULL)
{
p1 = malloc(sizeof(lista_notas));//y si usamos calloc ¿qué cambia?
if (p1 == NULL)
exit(71);
if (p1 != NULL)
{
p1->nombre = name;
p1->apellido = surname;
p1->nota = grade;
p1->siguiente = NULL;
*ptr = p1;
}
}
else
{
while(p1->siguiente != NULL)//recorrer la lista hasta el último
p1 = p1->siguiente;
p2 = malloc(sizeof(lista_notas));//y si usamos calloc ¿qué cambia?
if (p2 == NULL)
exit(71);
if(p2 != NULL)
{
p2->nombre = name;
p2->apellido = surname;
p2->nota = grade;
p2->siguiente = NULL;
p1->siguiente = p2;
}
}
}
void mostrar_lista(lista_notas *ptr)
{
while(ptr != NULL)
{
printf("%s ",ptr->nombre);
printf("%s ",ptr->apellido);
printf("%d\n",ptr->nota);
ptr = ptr->siguiente;
}
/* printf("\n");*/
}
void free_lista(lista_notas *ptr)
{
lista_notas *aux = ptr;
lista_notas *aux2;
while(aux != NULL)
{
aux2 = aux;
aux = aux->siguiente;
free(aux2->nombre);
free(aux2->apellido);
free(aux2);
}
}
int get_grade(char *line)
{
int i, blank, num;
i = 0;
blank = 0;
while(line[i] != '\0')
{
if (blank == 2) //sacar la nota
{
num = atoi(&line[i]);
blank++;
}
else if(line[i] == ' ')
blank++;
i++;
}
return num;
}
char *get_name(char *line)
{
int i;
char *name = malloc(51);
if (name == NULL)
exit(71);
i = 0;
while(line[i] != ' ')
name[i] = line[i++];
name[i] = 0;
return name;
}
char *get_surname(char *line)
{
int i, j;
char *surname = malloc(101);
if (surname == NULL)
exit(71);
i = 0;
j = 0;
while(line[i] != ' ')
i++;
while(line[++i] != ' ')
surname[j++] = line[i];
surname[j] = 0;
return surname;
}
At least these problems:
++ not sequenced
name[i] = line[i++]; is bad code. The i++may occur before, after, (or even during) name[i]. Result: undefined behavior (UB). #paddy
Limited size
char *name = malloc(51); does not need to use the magic number 51.
May run off end
while(line[i] != ' ') leads to trouble when line[] lacks a ' '.
Alternative (like OP's code)
char *get_name(char *line) {
size_t i = 0;
while(line[i] != ' ' && line[i] != '\0') {
i++;
}
char *name = malloc(i + 1);
if (name == NULL) {
exit(71);
}
for (size_t j = 0; j < i; j++) {
name[j] = line[j];
}
name[i] = 0;
return name;
}
Or perhaps using standard efficient library functions
char *get_name(const char *line) {
size_t token_length = strcspn(line, " ");
char *name = malloc(token_length + 1);
if (name == NULL) {
exit(71); // Consider a named macro/enum here rather than a magic 71
}
name[token_length] = '\0';
return memcpy(name, line, token_length);
}
Other issues
Off by 1
// fgets(line, MaxLinea, archivo)
fgets(line, sizeof line, archivo)
Advanced: Assuming first, last names do not include spaces
Consider first names like "Betty Jo" and last names like Lloyd Webber
Some names are longer than 51.
I am writing a simple program that at first checks a file for the number of structures present and allocates memory accordingly, then allows the user to insert new registries and allocates memory for those as well. Before program is terminated, all the data is saved to a binary file. I am able to insert up to 8 "servidor", but no more. When debugging, I found that after 8 entries, the program fails to realloc for a 9th, which indicates memory problems.
Call Stack
ntdll.dll!ntdll!RtlRaiseException (Unknown Source:0)
ntdll.dll!ntdll!RtlIsZeroMemory (Unknown Source:0)
ntdll.dll!ntdll!RtlIsZeroMemory (Unknown Source:0)
ntdll.dll!ntdll!.misaligned_access (Unknown Source:0)
ntdll.dll!ntdll!.misaligned_access (Unknown Source:0)
ntdll.dll!ntdll!.misaligned_access (Unknown Source:0)
ntdll.dll!ntdll!RtlReAllocateHeap (Unknown Source:0)
ntdll.dll!ntdll!RtlReAllocateHeap (Unknown Source:0)
AcLayers.dll!NotifyShims (Unknown Source:0)
msvcrt.dll!realloc (Unknown Source:0)
main() (c:\Users\tiago\Desktop\c.c:145)
Steps to reproduce
Compile
Run
Insert many registries by typing 1 and typing a name.
Program should fail at about 8 insertions, reverting file back to previous saved state, or 0.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef struct Servidor
{
bool ocupado;
int codigo;
char nome[256];
} servidor;
int escrever_arquivo(void * _struct, size_t tam_struct, int tamanho)
{
FILE *fptr = fopen("data.bin", "w");
if(fptr == NULL)
{
return 1;
}
fwrite(_struct, tam_struct * tamanho, 1, fptr);
fclose(fptr);
}
int ler_arquivo(void * _struct, size_t tam_struct, int tamanho)
{
FILE *fptr = fopen("data.bin", "r");
if(fptr == NULL)
{
return 1;
}
fread(_struct, tam_struct * tamanho, 1, fptr);
fclose(fptr);
}
int busca_tamanho()
{
FILE *fptr = fopen("data.bin", "a");
if(fptr == NULL)
{
printf("Erro na abertura do arquivo!\n");
exit(1);
}
fseek(fptr, 0L, SEEK_END);
int tamanho = ftell(fptr);
fclose(fptr);
return tamanho / sizeof(servidor);
}
int busca_livre(servidor * grupo, int tamanho)
{
for(int i = 0; i < tamanho; i++)
{
if(grupo[i].ocupado != true)
{
return i;
}
}
}
void inserir_servidor(servidor * grupo, int tamanho)
{
if(!tamanho)
{
grupo[0].ocupado = true;
grupo[0].codigo = 0;
printf("Nome: ");
fgets(grupo[0].nome, sizeof(grupo[0].nome), stdin);
fflush(stdin);
}
else
{
int pos = busca_livre(grupo, tamanho);
grupo[pos].ocupado = true;
grupo[pos].codigo = pos;
printf("Nome: ");
fgets(grupo[pos].nome, sizeof(grupo[pos].nome), stdin);
fflush(stdin);
}
}
void imprimir_servidor(servidor * grupo, int tamanho)
{
for(int i = 0; i < tamanho; i++)
{
printf("%s %d\n\n", grupo[i].nome, grupo[i].codigo);
}
}
int main()
{
servidor * grupo;
int tamanho = busca_tamanho();
int memoria = tamanho * sizeof(servidor);
if(tamanho)
{
grupo = malloc(memoria);
ler_arquivo(grupo, sizeof(servidor), tamanho);
}
char input;
printf("memoria inicial: %d B , tamanho: %d\n\n", memoria, tamanho);
do
{
printf("1. Inserir servidor\n");
printf("2. Listar servidores\n");
printf("0. Sair do programa\n\n");
printf("> ");
scanf("%c", &input);
fflush(stdin);
switch(input)
{
case '0':
escrever_arquivo(grupo, sizeof(servidor), tamanho);
free(grupo);
printf("Saindo do programa\n");
return 0;
case '1':
if(!tamanho)
{
grupo = malloc(sizeof(servidor));
memoria += sizeof(servidor);
tamanho++;
inserir_servidor(grupo, tamanho);
}
else
{
realloc(grupo, memoria + sizeof(servidor));
memoria += sizeof(servidor);
tamanho++;
inserir_servidor(grupo, tamanho);
}
printf("memoria em uso: %d B , tamanho: %d\n\n", memoria, tamanho);
break;
case '2':
imprimir_servidor(grupo, tamanho);
break;
default:
printf("Inexistente\n");
break;
}
} while(input != '0');
return 0;
}
I have a problem in the fprintf in the function "menor" because the data is not visible in the file selected, i dont know what is the problem, please help, i think is the reference file, but not idea.
I have a problem in the fprintf in the function "menor" because the data is not visible in the file selected, i dont know what is the problem, please help, i think is the reference file, but not idea
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
typedef struct {
char * nombre;
char * editorial;
int valor;
int area;
int vendido;
}
Libro;
int conteo(FILE * _entrada);
void datos(FILE * _entrada, Libro * _trabajo, int cantidad);
void menor(FILE * _entrada, FILE * _salida, Libro * _trabajo, int cantidad);
int main() {
FILE * entrada;
char ingreso[256];
printf("Direccion del archivo de entrada \n");
scanf("%255s", ingreso);
entrada = fopen(ingreso, "r");
if (entrada == NULL) {
printf("No se pudo acceder al archivo de entrada \n");
exit(1);
}
FILE * salida;
char final[256];
printf("Direccion del archivo de salida \n");
scanf("%255s", final);
salida = fopen(final, "r");
if (salida == NULL) {
printf("No se pudo acceder al archivo de salida \n");
exit(1);
}
int cantidad = conteo(entrada);
Libro * trabajo;
trabajo = (Libro * ) malloc(sizeof(Libro) * cantidad);
datos(entrada, trabajo, cantidad);
menor(entrada, salida, trabajo, cantidad);
return 0;
}
int conteo(FILE * _entrada) {
char auxiliar1[100];
int conteo = 0;
while (!feof(_entrada)) {
fgets(auxiliar1, 100, _entrada);
conteo++;
}
rewind(_entrada);
return (conteo / 5);
}
void datos(FILE * _entrada, Libro * _trabajo, int cantidad) {
char auxiliar2[100];
char * token;
while (!feof(_entrada)) {
fgets(auxiliar2, 100, _entrada);
token = strtok(auxiliar2, ":");
token = strtok(NULL, ":");
(_trabajo - > nombre) = strdup(strtok(token, "\n"));
fgets(auxiliar2, 100, _entrada);
token = strtok(auxiliar2, ":");
token = strtok(NULL, ":");
(_trabajo - > editorial) = strdup(strtok(token, "\n"));
fgets(auxiliar2, 100, _entrada);
token = strtok(auxiliar2, ":");
(_trabajo - > valor) = atoi(strtok(NULL, ":"));
fgets(auxiliar2, 100, _entrada);
token = strtok(auxiliar2, ":");
(_trabajo - > area) = atoi(strtok(NULL, ":"));
fgets(auxiliar2, 100, _entrada);
token = strtok(auxiliar2, ":");
(_trabajo - > vendido) = atoi(strtok(NULL, ":"));
}
}
void menor(FILE * _entrada, FILE * _salida, Libro * _trabajo, int cantidad) {
int i;
int menor = 0;
int posicion = 0;
for (i = 0; i < cantidad; i++) {
if ((_trabajo - > area) == 1) {
menor = (_trabajo - > vendido);
_trabajo++;
}
}
_trabajo--;
fprintf(_salida, "%s", (_trabajo - > nombre));
}
In main, change:
salida = fopen(final, "r");
Into:
salida = fopen(final, "w");
This will truncate the file. If you wanted to update the file (meaning change the existing file's contents without deleting it), you'd want "r+".
Your description of the problem is vague. Please update your question to show us the exact (copy-and-pasted) error message.
But when I compile your program, I get:
c.c:1:26: fatal error: stdio.h : No such file or directory
#include < stdio.h >
^
compilation terminated.
Remove the spaces from the #include directives. Change this:
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
to this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
(The interpretation of the name between < and > is compiler-specific, but in this case the compiler is probably looking for a file whose name is literally " stdio.h ", including the leading and trailing spaces.)
I want to read some values from a file using a function and pass them to main.
The file has specific format:
string double char
For example 2 lines:
blahblah 0.12 G
testtesttest 0.33 E
I have the following program. Although values are printed correctly in the function, in main only a few of them are printed. The rest are 0.00000 and no character is printed as well. What am I doing wrong?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int read_file(const char *filename, double **prob, char **sense);
int main(){
double *iprob;
char *sense;
read_file("test.txt", &iprob, &sense);
printf("Main: %lf %c\n", iprob[0], sense[0]);
return 0;
}
int read_file(const char *filename, double **prob, char **sense){
FILE *fp;
char line[100], temp[80];
int i = 0;
fp = fopen(filename, "r");
if (fp == NULL){
fprintf(stderr,"File %s not found!\n", filename);
return 0;
}
//*prob = (double *)malloc(sizeof(double) * 100);
//*sense = (char *)malloc(sizeof(char) * 100);
while( fgets(line, 100, fp) != NULL){
prob[i] = (double *)malloc(sizeof(double));
sense[i] = (char *)malloc(sizeof(char));
if ( sscanf(line, "%s %lf %c", temp, prob[i], sense[i]) < 3 ){
fprintf(stderr, "Parsing error detected at line %d!", i);
fclose(fp);
return 0;
}
else{
printf("%lf %c\n", *prob[i], *sense[i]);
}
i++;
}
fclose(fp);
return 1;
}
You use a double pointer to double in your functions, because you want to update the pointer passed in from main.
The problem is, that the allocated array is in *prob, and therefore you have to address the elements of that array as (*prob)[i].
*prob[i] is the same as *(prob[i]). prob is the pointer to a pointer; it has only one element, so to speak, so any index except 0 is invalid here.
Below is a correction of your code:
It reads in as many entries as there are in the file by reallocating memory as needed.
It returns -1 on failure and the number of items when successful, so you know how many items you can safely address.
You should free both pointers after use.
So:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_file(const char *filename, double **prob, char **sense);
int main(){
double *iprob = NULL;
char *sense = NULL;
int i, n;
n = read_file("test.txt", &iprob, &sense);
for (i = 0; i < n; i++) {
printf("Main: %lf %c\n", iprob[i], sense[i]);
}
free(iprob);
free(sense);
return 0;
}
int read_file(const char *filename, double **prob, char **sense){
FILE *fp;
char line[100];
int size = 0;
int i = 0;
*prob = NULL;
*sense = NULL;
fp = fopen(filename, "r");
if (fp == NULL) return -1;
while (fgets(line, sizeof(line), fp) != NULL) {
char temp[80];
if (i >= size) {
size += 8;
*prob = realloc(*prob, size * sizeof(**prob));
*sense = realloc(*sense, size * sizeof(**sense));
if (*prob == NULL || *sense == NULL) {
fclose(fp);
return -1;
}
}
if (sscanf(line, "%79s %lf %c", temp, &(*prob)[i], &(*sense)[i]) < 3) {
fprintf(stderr, "Parsing error detected at line %d!", i);
fclose(fp);
return -1;
}
printf("%lf %c\n", (*prob)[i], (*sense)[i]);
i++;
}
fclose(fp);
return i;
}
change to
*prob = (double *)malloc(sizeof(double) * 100);
*sense = (char *)malloc(sizeof(char) * 100);
while( fgets(line, 100, fp) != NULL){
//prob[i] = (double *)malloc(sizeof(double));
//sense[i] = (char *)malloc(sizeof(char));
if ( sscanf(line, "%s %lf %c", temp, &(*prob)[i], &(*sense)[i]) < 3 ){
fprintf(stderr, "Parsing error detected at line %d!", i);
fclose(fp);
return 0;
}
else{
printf("%lf %c\n", (*prob)[i], (*sense)[i]);
}
i++;
}