Search
 
SCRIPT & CODE EXAMPLE
 

CPP

read a file line by line c++ struct site:stackoverflow.com

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char name[100];
    int age;
    float height;
} PERSON;

int
main()
{
    PERSON *X = NULL;
    FILE *f;
    int i;
    PERSON *p;
    size_t count = 0;
    size_t alloc = 0;

    f = fopen("filename.txt", "r");

    while (1) {
        if (count >= alloc) {
            alloc += 100;
            X = realloc(X,sizeof(PERSON) * alloc);
        }

        p = &X[count];

        if (fscanf(f, "%s%d%f", p->name, &p->age, &p->height) == EOF)
            break;

        ++count;
    }

    fclose(f);

    // trim the array to what was actually used
    X = realloc(X,sizeof(PERSON) * count);

    p = X;
    for (i = 0;  i < count;  i++, p++)
        printf("%s
%d 
%f
", p->name, p->age, p->height);

    return 0;
}
Comment

read a file line by line c++ struct site:stackoverflow.com

#include <stdio.h>

// struct person with 4 fields
    struct person
    {
        char name[100];
        char address[100];
        char IDnumber[20];
        int  age;
    };

int main()
{
   FILE *file = fopen ("personout.txt","r");
   // declares an struct array to store data
   struct person student[10];

   int k=0;

   if ( file != NULL )    
   {
       char line [ 128 ]; /* or other suitable maximum line size */
       while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
       {
           // Returns first token
           char* token = strtok(line,",");

          // Keep printing tokens while one of the
          // delimiters present in str[].
          while (token != NULL) {
              k=0;
           // countword++;
              printf("%s
", token);

              switch (k) {
              case '1':
                  student[k].name==token;
              case '2':
                  student[k].address==token;
              case '3':
                  student[k].IDnumber==token;
              case'4':
                  student[k].age==token;
              default:
                  break;
              }

              k++;

              // put the values to array
              token = strtok(NULL, ",");
          }
      }
      printf("%s
", student[k].name);
      fclose ( file );
  }

}
Comment

PREVIOUS NEXT
Code Example
Cpp :: print numbers after decimal point c++ 
Cpp :: recherche recursive le max dans une liste 
Cpp :: c++ convert int to string with a fixed number of digits 
Cpp :: even or odd program in c++ 
Cpp :: Maximum Weight Difference codechef solution c++ 
Cpp :: graph colouring 
Cpp :: surf interpolation matlab 
Cpp :: 1822. Sign of the Product of an Array leetcode in c++ 
Cpp :: C++ Initializing a thread with a class/object 
Cpp :: how to create windows warning message c++ 
Cpp :: remove a element from an array c++ 
Cpp :: void setup() { // put your setup code here, to run once:in m}void loop() { // put your main code here, to run repeatedly:} 
Cpp :: glUniform bool 
Cpp :: how to print double value up to 9 decimal places in c++ 
Cpp :: define for loop c++ 
Cpp :: is obje file binary?? 
Cpp :: default parameter c++ a field 
Cpp :: logisch nicht 
Cpp :: destiny child 
Cpp :: Hash Sort in C++ 
Cpp :: pagesNumbering C++ 
Cpp :: KUNG FU HUSTLE 
Cpp :: c++ insertion in astack 
Cpp :: how to move your chrector in unity 
Cpp :: sfml get position 
Cpp :: how to writte comment in c++ 
Cpp :: c++ read_ascii 
Cpp :: Marin and Photoshoot codeforces solution in c++ 
Cpp :: fibonacci search algorithm c++ 
Cpp :: c + + to c converter 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =