Thread: Newbie To C
View Single Post
Old Nov 28th, 2004, 1:57 PM   #1
Yammer
Newbie
 
Join Date: Nov 2004
Posts: 3
Rep Power: 0 Yammer is on a distinguished road
Okay when I complie this using Miracle C as the complier, it will complie, build and run. When DOS opens I try to type and then DOS Closes. Any suggestions?

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

// define a global structure to contain the data in the input file
struct person
{
  char name[50];
  char hometown[50];
  char homestate[3];
  char occupation[50];
};

void main()
{
  FILE *infile;
  struct person people[10]; // allocate an array of structures to hold data from the input file
  int i = 0; // counter for the array index
  int j = 0; // counter for display loop
  
  // open file for reading. This opens the file and returns a pointer that
  // points to the first character in the file. This opens the file as
  // a sequential file for read only.
  infile = fopen("infile.txt","r");
  
  // cycle through the input file and put the data into an array
  while (feof(infile) == 0)
  {
   fscanf(infile," %s ", people[i].name);
   fscanf(infile," %s ", people[i].hometown);
   fscanf(infile," %s ", people[i].homestate);
   fscanf(infile," %s ", people[i].occupation);
   i++; // increment for next time through the loop
  }
  
  // display contents of array back to console
  
  for (j = 0; j < i; j++) // remember that i has the number of entries in the array
  {
   printf("%s %s %s %s\n", people[j].name, people[j].hometown, people[j].homestate, people[j].occupation);
  }
  
  // close the file
  fclose(infile);
  
  getch(); // pause program to see output
}
Yammer is offline   Reply With Quote