Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC