Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_LINE_LEN 10000
- #define MAX_NO_LINES 100
- /*
- * On success, the function returns str.
- If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof).
- If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
- If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).
- *
- */
- /*
- * void* malloc (size_t size);
- *
- *
- */
- int main() {
- FILE * f = fopen("../in.txt", "r");
- char **tab = (char **)malloc(sizeof(char*)*MAX_NO_LINES);
- if (f == NULL)
- return 100;
- char* linie = (char *)malloc(sizeof(char)* MAX_LINE_LEN);
- char* ok=linie;
- int n = 0;
- while (ok == linie){
- ok = fgets(linie, MAX_LINE_LEN, f);
- tab[n] = (char*)malloc(sizeof(char) * (strlen(linie) + 1));
- strcpy(tab[n], linie);
- n++;
- }
- free(linie);
- for(int i=0;i<n;i++){
- printf("%s", tab[i]);
- }
- fclose(f);
- return 0;
- }
RAW Paste Data