Using Malloc to Read a File Dynamically

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    File
Click For Summary
SUMMARY

The discussion centers on a C program that uses dynamic memory allocation with malloc and realloc to read integers from a file named "list2.txt". The primary issue identified is an infinite loop caused by the fscanf function not correctly handling the comma delimiter in the input file. Participants suggest checking the value of the variable 'inp' to ensure it does not equal a comma, as this would prevent proper reading of integers. The code requires modification to handle non-integer characters appropriately to avoid the infinite loop.

PREREQUISITES
  • Understanding of C programming language
  • Knowledge of dynamic memory management using malloc and realloc
  • Familiarity with file I/O operations in C
  • Basic understanding of the fscanf function and its behavior with different data types
NEXT STEPS
  • Learn how to properly handle delimiters in file input using fscanf
  • Explore error handling techniques for file operations in C
  • Investigate alternatives to fscanf for reading formatted input, such as getline
  • Study dynamic memory allocation best practices to prevent memory leaks
USEFUL FOR

C programmers, software developers dealing with file I/O, and anyone interested in dynamic memory management in C.

anonim
Messages
39
Reaction score
2
Homework Statement
Read a file dynamically
Relevant Equations
-
C:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>
int main(){
        
    int index=1;
    int *arr;
    int data;
    char *c;
    FILE *fp;
    int input,inp;
    arr=(int*)malloc(sizeof(int));
    fp=fopen("list2.txt","r");
    input=fscanf(fp,"%d",&inp);
    while(input!=EOF){
        printf("%d",inp);
        if(inp!=','){
            arr[index-1]=inp;
            index++;
            arr=realloc(arr,(index)*sizeof(int));
        }
        input=fscanf(fp,"%d",&inp);
    }
    fclose(fp);
    free(arr);
    return 0;
}
I want to read a file that file contains-> 1,2,3,5,7,888, But my code goes into loop, why? How can I fixed this?
 
Physics news on Phys.org
I would put print statement into see what value of input is being returned and what the value of EOF is.

The while loop will only terminate when input==EOF

Also I would look at the inp value to see if it matches the number your expect. Will inp==',' when you run the program or will you have to read the ',' to get to the next number?

Here's an example that reads multiple data types:

https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rtref/fscanf.htm
 
@anonim, your code is supposed to go into a loop. Do you mean an infinite loop? What output do you get before your code goes into an infinite loop, assuming that's what you meant?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 1 ·
Replies
1
Views
3K