For starters, you need to format your code properly, especially by using proper indentation. For example:
Code:
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
struct contents
{
int source, destination, type, port;
char data[50];
};
struct contents s, d, t, p, x;
int main()
{
FILE *inFile;
charinFileName[30] = {'\0'};
printf("Enter file name: ");
scanf("%s", inFileName);
printf("\n");
printf("Opening %s . . .\n", inFileName);
printf("\n");
if ((inFile = fopen(inFileName,"r")) == NULL)
{
printf("Error: Could not open %s\n", inFileName);
exit(1);
}
/* ...and now I'm stuck */
fclose(inFile);
return 0;
}
(I did not put in any extra syntax highlighting; the forum software will do syntax highlighting for you so you should not add in your own like what you did.)
You should avoid global variables. Furthermore, you should use descriptive names for your variables, especially when they are not within some small scope.
Now, if you want to read in the filename with scanf, that can be done, but you need to avoid buffer overflow by specifying the field width. You should also check the return value of scanf. For example:
Code:
if (scanf("%29s", inFileName) != 1)
{
printf("Error: invalid filename\n");
return 0;
}
Next, to actually read from the file, it looks like you should read line by line. It seems like each line will have 20 + N characters, where N is up to a maximum of 49, seeing that you're going to use a 50 character array to store that 5th part. If so, you could use say, a 71 character array (20 + 49 + 1 for newline + 1 for null character), reading into it using fgets, then parse the line read into a struct contents object with sscanf. You might want an array of struct contents objects, or perhaps a single one will do, depending on your requirements (but whether array or lone object, it should be local to the main function).