Using Malloc to Read a File Dynamically

  • Comp Sci
  • Thread starter anonim
  • Start date
  • Tags
    File
In summary, the conversation is about a code that is meant to read a file containing numbers, but is going into a loop. The expert suggests adding print statements to check the values of input and EOF, as well as the inp value to see if it matches the expected number. They also provide a link to an example of reading multiple data types.
  • #1
anonim
40
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
  • #2
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
 
  • #3
@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?
 

FAQ: Using Malloc to Read a File Dynamically

How does using malloc to read a file dynamically differ from traditional file reading methods?

Using malloc allows for dynamic memory allocation, meaning that the amount of memory allocated can be adjusted based on the size of the file being read. This allows for more efficient use of memory and can be particularly useful when dealing with large files.

What are the advantages of using malloc to read a file dynamically?

One advantage is that it can save memory space by only allocating the necessary amount of memory for the file being read. Additionally, it can be more efficient for reading large files as it avoids allocating a fixed amount of memory that may not be enough.

Are there any potential drawbacks to using malloc for file reading?

One potential drawback is the need for proper error handling. If the file being read is not closed properly or if there are any errors in the reading process, it can lead to memory leaks. Additionally, using malloc can be more complex and may require more coding than traditional file reading methods.

How can I use malloc to read a file dynamically in my code?

First, you will need to declare a pointer to store the data read from the file. Then, use the malloc function to allocate memory for the file based on its size. Next, use a loop to read the file and store the data in the allocated memory. Finally, remember to free the allocated memory once you are finished using it.

Is using malloc to read a file dynamically necessary for all file reading operations?

No, it is not necessary for all file reading operations. It is most useful when dealing with large files or when the size of the file is unknown. For smaller files or files with a known size, traditional file reading methods may be more appropriate.

Similar threads

Replies
3
Views
975
Replies
4
Views
1K
Replies
4
Views
2K
Replies
3
Views
908
Replies
11
Views
2K
Replies
3
Views
1K
Replies
1
Views
9K
Replies
1
Views
2K
Back
Top