reading text file into struct array through dos

so i made this code made already where it would take it files from I/O redirection, now i need to make it work through dos.

where you assign the input file name, and the output file name, i dont need it to display to the user anymore.

i just cant get it to read the the content and put it into arrays. it gives me an error for every array saying that "this does not work in dos mode"


EDIT***NEWEST CODE***
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121


/*
This programs takes input through ioredirection with the structure of
name
address
citystate
zip
and arranges them by zipcode
MAKNELTEK
5/26/2009
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int TOTAL = 18;

typedef struct info {
    char name[30];
    char address[30];
    char citystate[30];
    char zip[30];
}a;

void get_info(struct info *a, FILE *fn);
void sortdata( struct info *a);
void write_out(struct info *a, FILE *fo);
char text[100], text2[100];

void main(void)
{
	printf ("Enter a filename to load: ");
    scanf ("%s" , &text);
	printf ("Enter output file name: ");
	scanf ("%s" , &text2);
	FILE *fn, *fo;
	fn = fopen(text, "r");
	if (fn == NULL)																	/*error checking*/
	{
		printf ("Cannot open file %s \n", text);
		exit (0);
	}
	fo = fopen(text2, "w");
	if (fo == NULL)																	/*error checking*/
	{
		printf ("Cannot open file %s", text2);
	}
	info *spc;
	spc = (info*)malloc (sizeof(info));
	get_info(spc, fn);
	sortdata(spc);
	write_out(spc, fo);
	fclose (fn);
	fclose (fo);
}

void get_info(info *a, FILE *fn)													/*pulls all info*/
{
	while ( !feof(fn))
	{
		char *p;
		for ( int i = 0; i <TOTAL; i++){
			fgets(a[i].name, sizeof(a[i].name), fn);								/*records name*/
			if ((p = strchr(a[i].name, '\n')) != (NULL))
				*p = '\0';
			fgets (a[i].address, sizeof(a[i].address), fn);							/*records address*/
			if ((p = strchr(a[i].address, '\n')) != (NULL))
				*p = '\0';
			fgets (a[i].citystate, sizeof(a[i].citystate), fn);						/*records city state*/
			if ((p = strchr(a[i].citystate, '\n')) != (NULL))
				*p = '\0';
			fgets (a[i].zip, sizeof(a[i].zip),fn);									/*record zip*/
			if ((p = strchr(a[i].zip, '\n')) != (NULL))
				*p = '\0';
		}
	}
}

void write_out(struct info *a, FILE *fo)											/*Writes out to file*/
{
	for ( int i = 0; i <TOTAL; i++)
	{
		fputs(a[i].name, fo);
		fputc ('\n', fo );
		fputs (a[i].address, fo);
		fputc ('\n', fo );
		fputs (a[i].citystate, fo);
		fputc ('\n', fo );
		fputs (a[i].zip,fo);
		fputc ('\n', fo );
	}
}

void sortdata(info *a)
{    
    int t;
    for (t=0; t<TOTAL; t++)
    {
        int i;
        for ( i = t; i < TOTAL; i++)
        {
            if (-1==(strcmp(a[i].zip,a[t].zip)))
            {																		/*swaps entire data*/
                char temp[30];
                memcpy(temp, a[i].zip, sizeof(a[i].zip));							/*swaps zip*/
                memcpy(a[i].zip, a[t].zip, sizeof(a[t].zip));
                memcpy(a[t].zip, temp, sizeof(temp));
                memcpy(temp, a[i].name, sizeof(a[i].name));							/*swaps name*/
                memcpy(a[i].name, a[t].name, sizeof(a[t].name));
                memcpy(a[t].name, temp, sizeof(temp));
                memcpy(temp, a[i].address, sizeof(a[i].address));					/*swaps address*/
                memcpy(a[i].address, a[t].address, sizeof(a[t].address));
                memcpy(a[t].address, temp, sizeof(temp));
                memcpy(temp, a[i].citystate, sizeof(a[i].citystate));				/*swaps city-state*/
                memcpy(a[i].citystate, a[t].citystate, sizeof(a[t].citystate));
                memcpy(a[t].citystate, temp, sizeof(temp));
            }
        }
    }
}
Last edited on
Modern compilers no longer generate code for DOS, AFAIK. If you need to compile something to run in 16-bit mode, try using an older compiler. VC++ 6.0 should work.

Do you absolutely need to run from pure DOS?
well this is whats supposed to happen.

we create the program,

the from the dos window, we open the file.. say..


-----------------------------------
c:\zipcodes.exe
What file would you like to open?
address.txt
file to output to?
out.txt

DONE!!

press any key to continue.
----------------------------------

address and out are user inputs through dos.

also, in school we do use 6.0, im using 2008 express edition..

but proff never mentioned anything about editions diferences.

////////EDIT///////
i reposted it, it works, it reads the information, sorts it well, and prints it to screen, but at the very end of sorting and outputting to the screen, it freezes. i get no error message, it just says it stoped working.
Last edited on
Well, that's one of the differences. 6.0 and 2008 are ten years apart, so they are quite different.

That's odd. If you can run 2008, you should be able to run anything it generates, IINM. You're running an NT kernel, right?

EDIT: You're accessing memory you shouldn't be accessing. I'm too tired to check your array boundaries, but perhaps someone else might want to.
Last edited on
so i got it working most of it.

but it only works though visual studio "ctrl+f5" debbuging mode. works great.

but when i access it through dos, it stalls.

anyone know why.

i updated the code to what i currelt have
Topic archived. No new replies allowed.