visoft

Citire linii din fisiere

Nov 5th, 2020
1,154
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. /*
  6.  *  fscanf This function returns the number of input items successfully
  7.  * matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure
  8.  *
  9.  *
  10.  * long int ftell ( FILE * stream );
  11. Get current position in stream
  12. Returns the current value of the position indicator of the stream.
  13.  
  14. For binary streams, this is the number of bytes from the beginning of the file.
  15.  
  16. For text streams, the numerical value may not be meaningful but can still
  17.  be used to restore the position to the same position later using fseek (if there are characters put
  18.  back using ungetc still pending of being read, the behavior is undefined).
  19.  *
  20.  *
  21.  *
  22.  * char * fgets ( char * str, int num, FILE * stream );
  23. Get string from stream
  24. Reads characters from stream and stores them as a C string into str until (num-1)
  25.  characters have been read or either a newline or the end-of-file is reached, whichever happens first.
  26.  
  27. A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
  28.  *
  29.  *
  30.  * On success, the function returns str.
  31. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof).
  32.  If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
  33. 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).
  34.  *
  35.  */
  36. #define MAX_LEN 500
  37. int main() {
  38.     FILE*f;
  39.     f = fopen("../in.txt", "r");
  40.     if (f == NULL){
  41.         return 66;
  42.     }
  43.  
  44.     int n=100;
  45.     char *tab[1000];
  46.  
  47. //    fscanf(f, "%d", &n1);
  48. //    fscanf(f, "%d", &n2);
  49. //    printf("%d\n", n);
  50.  
  51.     int index_citit = 0;
  52.     int nr_potriviri = 0;
  53.     while(feof(f) == 0){
  54.         tab[index_citit] = (char*)malloc(sizeof(char) * MAX_LEN);
  55.         char*ret = fgets(tab[index_citit], MAX_LEN, f);
  56.         index_citit++;
  57.     }
  58.     printf("\n%s\n", tab[2]);
  59.     fclose(f);
  60.     for (int i=0; i< index_citit;i++)
  61.         free(tab[i]);
  62.     return 0;
  63. }
  64.  
  65.  
RAW Paste Data