![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 3
Rep Power: 0
![]() |
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
} |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Added code tags.
It looks like you're telling it to close after keyboard input. What exactly did you want to type?
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2004
Posts: 3
Rep Power: 0
![]() |
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! ![]() |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>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;
}#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;
} |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2004
Posts: 3
Rep Power: 0
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|