Read All File Contents Into A Dynamically Allocated String | C Programming Example

preview_player
Показать описание
How to read and store all the contents of a file into a dynamically allocated string using C. An optimized solution that only requires reading the file once is created, after first creating a suboptimal but easier to understand solution that requires reading the file twice. A simple performance test is conducted to demonstrate the improved performance of the optimized solution.

Рекомендации по теме
Комментарии
Автор

Some of the best, most straightforward C tutorials on YouTube!

olalha
Автор

Thank you so much for the explanation and simple, easy to follow examples you provide. I managed to score A on my final exam only because of your videos. Immensely appreciated! Easy SUB!

liolio
Автор

Instant file size result even on an old XT 8088:

fseek(file, 0L, SEEK_END);
total = ftell(file);
rewind(file);

But it's nice that you gave the viewers a memory management bonus lesson. :)

Brock-Landers
Автор

I think using stat, calloc, and fread might be even more optimized.

But I like the way you take us through this tutorial. Just subscribed to the channel.
👍

airrsongs
Автор

Weldone boss. I love you.

kindly consider do an explanatory video about bit manipulations, their uses and how.. especially with number.. odd numbers, even numbers and other applications

D_Ladybug
Автор

bro you are so underrated came to your channel and got answers for those questions which i think that was none sense and finally getting 5th

technicalgamer
Автор

Hi, I wanted to ask if you have a char array, where we have stored lines from a file, is there a way to check the type of the elements stored? So, for example if you iterate through the char array, that contains lines from a file, you come across a digit, convert it to a double. I'm working on a calculator project in c, that requires me to push elements of type double onto a stack and perform operations on them, once we come across a string in the file. However, I'm having difficulties converting the lines to ints and pushing them onto the stack. Is there anyway you could help? Thanks.

atiyahussain
Автор

This video helped a lot so thank-you. However i'm trying to do this exact thing except store each line of the file into a 2D string array so each line is it's own string. Would it be better to just use the method you have shown then use strtok() with \0 as the delimiter or is there a better way to go about this? Thanks Subscribed

funnymonkeypicture
Автор

How do we make a string array for each line instead of just one big string?

yigitbulut
Автор

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct studenti
{
char ime[20], prezime[20];
int ocena;
double prosek;
struct studenti *sledeci;
}STUDENTI;
STUDENTI *napravi(char *ime, char *prezime, int ocena)
{
STUDENTI
strcpy(novi->ime, ime);
strcpy(novi->prezime, prezime);
novi->ocena=ocena;
novi->sledeci=NULL;
return novi;
}
STUDENTI *dodaj(STUDENTI *glava, char *ime, char *prezime, int ocena)
{

STUDENTI *novi=napravi(ime, prezime, ocena);
if (glava==NULL)
{
glava=novi;
return glava;
}

glava->sledeci=novi;
}
STUDENTI *ucitaj(STUDENTI *glava, char*fajl)
{
FILE *fp=fopen(fajl, "r");
char linija[150], ime[25], prezime[25];
int ocena;
int pr=0;
int suma=0;
while(fgets(linija, sizeof(linija), fp))
{
char *token=strtok(linija, " ");
int br=0;
while(token!=NULL)
{
if(br==0) strcpy(ime, token);
if(br==1) strcpy(prezime, token);
if(br==2)
{
ocena=atoi(token);
pr++;
suma=suma+ocena;
}
br++;
token=strtok(NULL, " ");
}
glava=dodaj(glava, ime, prezime, ocena);
}
return(glava);
}
void printaj(STUDENTI *glava)
{
if (glava==NULL)
{

return;
}
printf("%s %s %d\n", glava->ime, glava->prezime, glava->ocena);
printaj(glava->sledeci);
}
void prosek(STUDENTI *glava)
{
double suma=0.0;
double prosek=0.0;
int br=0;
STUDENTI *tmp=glava;
while(tmp!=NULL)
{
br++;
suma+=tmp->ocena;
tmp=tmp->sledeci;
}
prosek=suma/br;
printf("Prosek je: %f", prosek);
}

ivanmartic