Zeilenweises auslesen einer txt Datei in C

    • Zeilenweises auslesen einer txt Datei in C

      Gibt es eine Funktion bzw. eine Möglichkeit, wie ich Integer Werte in eine txt Datei Zeilenweise reinschreibe, und das ganze dann Zeilenweise wieder (in z.B. ein Array) auslese?

      Bitte um schnelle Antwort! Ist dringend.

      mfg
      James
      [Blockierte Grafik: http://dl.dropbox.com/u/24753690/stuff/forenlinks/rocketsign.png]
      Team Rocket - so schnell wie das Licht,
      gebt lieber auf und bekämpft uns nicht!

      join #teamrocket @iz-smart.net
    • Schon mal was von fprintf und fscanf gehört?
      Ich hab mal ein kleines Beispiel zusammengeschustert:

      C-Quellcode

      1. #include <stdio.h>
      2. #include <malloc.h>
      3. typedef struct {
      4. int *array;
      5. int num;
      6. } tuple;
      7. void write_array(FILE *fp, tuple t)
      8. {
      9. int i = 0;
      10. for(; i < t.num; ++i) {
      11. fprintf(fp, "%d\n", t.array[i]);
      12. }
      13. fflush(fp);
      14. }
      15. tuple read_array(FILE *fp)
      16. {
      17. int num, i;
      18. tuple rw;
      19. fseek(fp, 0, SEEK_END);
      20. num = ftell(fp);
      21. fseek(fp, 0, SEEK_SET);
      22. rw.array = malloc(sizeof(int) * num);
      23. rw.num = num;
      24. for(i = 0; fscanf(fp, "%d\n", &rw.array[i]) != EOF; ++i);
      25. return rw;
      26. }
      27. int main(int argc, char *argv[])
      28. {
      29. FILE *fp = fopen("test.txt", "w");
      30. tuple tmp2;
      31. int tmp[5] = {1, 2, 3, 4, 5};
      32. int i;
      33. tmp2.array = tmp;
      34. tmp2.num = 5;
      35. write_array(fp, tmp2);
      36. fclose(fp);
      37. fp = fopen("test.txt", "r");
      38. tmp2 = read_array(fp);
      39. fclose(fp);
      40. for(i = 0; i < 5; ++i) {
      41. printf("%d\n", tmp2.array[i]);
      42. }
      43. free(tmp2.array);
      44. return 0;
      45. }
      Alles anzeigen
    • Danke erstmal für den Code

      Könntest du mir bitte Kommentare reineditieren?
      Weil mir einige Funktionen und Datentypen nicht ganz klar sind...

      z.B.: tuple oder malloc
      James
      [Blockierte Grafik: http://dl.dropbox.com/u/24753690/stuff/forenlinks/rocketsign.png]
      Team Rocket - so schnell wie das Licht,
      gebt lieber auf und bekämpft uns nicht!

      join #teamrocket @iz-smart.net
    • malloc steht für memory allocation, wodurch man sich speicher reserviert und einen pointer darauf legt, welcher zurückgegeben wird...


      aber um einfach nur zeilenweise einzulesen, genügt die funktion fgets

      die funktioniert so

      fgets(string, anzahl zeichen, dateipointer);

      string ist der string, auf welchem die gelesene zeile gespeichert wird

      anzahl zeichen ist die anzahl der maximal zu lesenden zeichen, also falls dein string begrenzt ist, das du nicht auf nen NULL pointer stößt^^

      und dateipointer ist selbsterklärend^^


      übrigends besitzt diese funktion einen rückgabewert, ob gelesen wurde oder nicht (1 oder 0) wodurch du am besten, wenn du aus einer datei liest, eine schleife drum baust, zB:

      while(fgets(ein, 1023, datei))

      die schleife arbeitet sich also durch die datei welche in der referenz von datei verbirgt durch, liest zeile für zeile raus und speichert sie in ein, (maximal 1023 zeichen, da ein ein string mit 1024 chars ist)
    • Öhm ich hab's mal kommentiert und gleich einen Fehler ausgebessert - hab halt schon lang nicht mehr C programmiert :/.

      C-Quellcode

      1. #include <stdio.h>
      2. #include <malloc.h>
      3. /**
      4. * This struct contains an array and the number of its elements.
      5. */
      6. typedef struct {
      7. int *array; /* the int-array */
      8. int num; /* the number of elements */
      9. } tuple;
      10. /**
      11. * write_array writes the array in the tuple struct into the file fp.
      12. *
      13. * @param fp is a file pointer.
      14. * @param t is a tuple which contains the array and the number of elements
      15. * which should be written into the file.
      16. */
      17. void write_array(FILE *fp, tuple t)
      18. {
      19. int i = 0;
      20. for(; i < t.num; ++i) {
      21. /* write the elements into the file */
      22. fprintf(fp, "%d\n", t.array[i]);
      23. }
      24. /* flush the file (all written elements are now written from the
      25. * internal buffer into the file) */
      26. fflush(fp);
      27. }
      28. /**
      29. * read_array reads the elements from a file into an int-array.
      30. *
      31. * @param fp is a pointer to a file
      32. *
      33. * @return a tuple struct is returned which contains a dynamically allocated
      34. * array and the number of elements in this array. Note: the array must be freed
      35. * by the user!
      36. */
      37. tuple read_array(FILE *fp)
      38. {
      39. int num, i;
      40. tuple rw;
      41. /* get the number of elements */
      42. for(num = 0; fscanf(fp, "%d\n", &i) != EOF; ++num);
      43. /* jump to the beginning of the file */
      44. fseek(fp, 0, SEEK_SET);
      45. /* allocate the array with the size num */
      46. rw.array = malloc(sizeof(int) * num);
      47. /* store the number of elements in the return value */
      48. rw.num = num;
      49. /* read the ints from the file */
      50. for(i = 0; fscanf(fp, "%d\n", &rw.array[i]) != EOF; ++i);
      51. return rw;
      52. }
      53. int main(int argc, char *argv[])
      54. {
      55. /* open the file */
      56. FILE *fp = fopen("test.txt", "w");
      57. tuple tmp2;
      58. int tmp[5] = {1, 2, 3, 4, 5};
      59. int i;
      60. /* first of all we are writing some dummy values (1-5) into the file */
      61. tmp2.array = tmp;
      62. tmp2.num = 5;
      63. /* write it */
      64. write_array(fp, tmp2);
      65. fclose(fp);
      66. /* now we are open the previously written file */
      67. fp = fopen("test.txt", "r");
      68. /* read the int-array from the file */
      69. tmp2 = read_array(fp);
      70. fclose(fp);
      71. /* print the int array */
      72. for(i = 0; i < tmp2.num; ++i) {
      73. printf("%d\n", tmp2.array[i]);
      74. }
      75. /* free the dynamically allocated array in the tuple struct */
      76. free(tmp2.array);
      77. return 0;
      78. }
      Alles anzeigen


      Zur Erklärung: malloc wurde ja schon erklärt. free ist sozusagen das Gegenstück zu malloc. Es gibt, wie der Name schon sagt, den von malloc dynamisch reservierten Speicher wieder frei. Immer wenn du irgendetwas mit malloc reservierst musst du es also irgendwann mal mit free wieder freigeben, ansonsten bekommst du Memory Leaks.

      tuple ist eine eigene definierte Datenstruktur, die eben das Array und dessen Größe beinhaltet. KA ob dir Strukturen schon geläufig sind, aber generell schaut so etwas so aus:

      Quellcode

      1. #include <stdio.h>
      2. typedef struct {
      3. int feld1;
      4. int feld2;
      5. } my_struct;
      6. void print_my_struct(my_struct *s)
      7. {
      8. printf("%d %d\n", s->feld1, s->feld2);
      9. }
      10. int main(int argc, char *argv[])
      11. {
      12. my_struct s;
      13. s.feld1 = 5;
      14. s.feld2 = 8;
      15. print_my_struct(&s);
      16. return 0;
      17. }
      Alles anzeigen

      In diesem Beispiel definiere ich meinen eigenen Datentyp my_struct, der aus 2 Feldern feld1 und feld2 besteht. Diese befülle ich dann im main und rufe dann print_my_struct mit einem pointer auf diese struct auf.
      print_my_struct gibt dann die beiden Felder mit s->feld1 (ist das selbe wie (*s).feld1) und s->feld2 aus.

      @fgets: fgets ist zwar OK wenn man generell strings zeilenweise einlesen will, aber in diesem Fall geht es doch im int-arrays oder? Dabei müsste ich doch jedes Mal zusätzlich noch atoi aufrufen und würde mir so eigentlich nicht wirklich etwas ersparen.
    • OK Danke!

      Habs jetzt bei meinem Programm eingebaut und es funktioniert.

      Von mir aus kann der Thread geschlossen werden.

      MFG
      James
      [Blockierte Grafik: http://dl.dropbox.com/u/24753690/stuff/forenlinks/rocketsign.png]
      Team Rocket - so schnell wie das Licht,
      gebt lieber auf und bekämpft uns nicht!

      join #teamrocket @iz-smart.net