Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2004, 12: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
Old Nov 28th, 2004, 1:12 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Added code tags.

It looks like you're telling it to close after keyboard input. What exactly did you want to type?
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Nov 28th, 2004, 1:44 PM   #3
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>When DOS opens I try to type and then DOS Closes.
Of course it does. The only interactive input your program asks for is the final pause at the end meant to keep the window open. All other input comes from a file.
Eggbert is offline   Reply With Quote
Old Nov 28th, 2004, 2:36 PM   #4
Yammer
Newbie
 
Join Date: Nov 2004
Posts: 3
Rep Power: 0 Yammer is on a distinguished road
Thanks, I'll have to get with my team and see waht the deal is on where the data file is.

one of my team members asked this question.

What if I use a "struct" to collect the information and then write it to file? (*fp) I'm trying to figure out if all the "get" statements can be linked into a struct.

Mjordan2nd- You are right, apparently it is supposed to bring up a data file that has certain parameters in it. Of course my team mates have not sent that file to me (ugh).

I am so lost with this! I do not think I shall go into programming as a profession or hobby, but who knows I may actually get into it and like it. But for now, I am frustrated with it. /kick yammer from progamming class!
Yammer is offline   Reply With Quote
Old Nov 28th, 2004, 3:02 PM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>But for now, I am frustrated with it.
The frustration doesn't go away, it just gives way to new frustrations as you learn more and become more confident. Though very few professions are as stimulating and rewarding.

>I'm trying to figure out if all the "get" statements can be linked into a struct.
The two options are to read each field individually as a line and assign them to the record, or read an entire formatted record and then parse it. Here is the former:
#include <stdio.h>

struct person {
 char name[50];
 char hometown[50];
 char homestate[4];
 char occupation[50];
};

int main ( void )
{
 struct person temp;

 while ( 1 ) {
  /* Read a record */
  if ( fgets ( temp.name, sizeof temp.name, stdin ) == NULL )
   break;
  fgets ( temp.hometown, sizeof temp.hometown, stdin );
  fgets ( temp.homestate, sizeof temp.homestate, stdin );
  fgets ( temp.occupation, sizeof temp.occupation, stdin );

  /* Print the record */
  printf ( "Name: %s", temp.name );
  printf ( "Hometown: %s", temp.hometown );
  printf ( "Homestate: %s", temp.homestate );
  printf ( "Occupation: %s\n", temp.occupation );
 }

 return 0;
}
And the latter:
#include <stdio.h>
#include <string.h>

struct person {
 char name[50];
 char hometown[50];
 char homestate[3];
 char occupation[50];
};

int main ( void )
{
 struct person temp;
 char buffer[BUFSIZ];

 while ( 1 ) {
  /* Read a line */
  if ( fgets ( buffer, sizeof buffer, stdin ) == NULL )
   break;

  /* Parse the line */
  strcpy ( temp.name, strtok ( buffer, ":" ) );
  strcpy ( temp.hometown, strtok ( NULL, ":" ) );
  strcpy ( temp.homestate, strtok ( NULL, ":" ) );
  strcpy ( temp.occupation, strtok ( NULL, ":" ) );

  /* Print the record */
  printf ( "Name: %s\n", temp.name );
  printf ( "Hometown: %s\n", temp.hometown );
  printf ( "Homestate: %s\n", temp.homestate );
  printf ( "Occupation: %s\n", temp.occupation );
 }

 return 0;
}
Please take note that the code I just gave is very unsafe in production code, and should be viewed as an example of concept, not a complete implementation.
Eggbert is offline   Reply With Quote
Old Nov 28th, 2004, 8:43 PM   #6
Yammer
Newbie
 
Join Date: Nov 2004
Posts: 3
Rep Power: 0 Yammer is on a distinguished road
Well thanks.
Appaerntly I got the worng code, but what you have given/explained to me helps a great deal. I'll look at the psuedo code again and see what I can develope from that.
Thanks again eggbert!
Yammer is offline   Reply With Quote
Reply

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 6:04 AM.

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