Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Newbie To C (http://www.programmingforums.org/showthread.php?t=1317)

Yammer Nov 28th, 2004 1:57 PM

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
}


Mjordan2nd Nov 28th, 2004 2:12 PM

Added code tags.

It looks like you're telling it to close after keyboard input. What exactly did you want to type?

Eggbert Nov 28th, 2004 2:44 PM

>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.

Yammer Nov 28th, 2004 3:36 PM

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! :mrgreen:

Eggbert Nov 28th, 2004 4:02 PM

>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.

Yammer Nov 28th, 2004 9:43 PM

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!


All times are GMT -5. The time now is 2:03 AM.

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