stuck with file i/o with C

Hi,

I'm having trouble with C, especially with string i/o and manip. I've gotten used to C++ with its string class and vectors, but having to learn C is a real pain. Getting confused by fgets, scanf, fputs, and compiler issues.

I would appreciate it if someone could guide me on storing a line of text into my array of struct
a quick example
i have 50 people, and lists several attributes, hair color, height, etc.
and I look into a way and found array of structs which fits because I can have 50 elements with 4 subelements to each (I assume that is how it works?)

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
#include <stdio.h>
#include <stdlib.h>
struct orderSlip
{
	char source[64];
	int position;
	char item[64];
	char destination[64];
};

void decision();
void read();
void choice();
int main()
{	
	//choice();
	decision();
	read();
	//pause
	return 0; 
}
void decision()
{
	printf("1 - Add Delivery Slip\n");
	printf("2 - Move Delivery Slip\n");
	printf("3 - Delete Delivery Slip\n");
	printf("4 - View Delivery Slips\n");
	printf("Please enter your choice: \n");
}

void choice()
{
	int choice;	
	scanf("%d", &choice);


}

void read()
{
	struct orderSlip data[100];
//test
	//strcpy_s(data[0].source, "next");

	static const char filename[] = "datafile.txt";

	FILE *file = fopen(filename, "r");
	
	if(file != NULL)
	{
		char line[64];

		while(fgets(line, sizeof line, file) != NULL)
		{
			fputs(line, stdout);
//how to put a line of text into my array of struct?

				
		}
	fclose(file);
	
	printf(data[0].source);
	}
	else
	{
		perror("You goofed!");
	
		exit(1);
	}
	
	
	

}
1
2
3
4
5
6
7
void choice()
{
	int choice;	
	scanf("%d", &choice);


}

This does absolutely nothing.


static const char filename[] = "datafile.txt";
You don't need to trouble yourself with static and const now you've just started to learn C :D

printf and scanf work with consoles: screen and keyboard respectively. fputs and fgets work in the same way but with files.


Can you please show us an example line from your datafile.txt?
Last edited on
my datafile example

4930 new york
apartment 101
package
0
3028 washington street
apartment 49
package
1

I am leaning towarads a struct of arrays to store this data, or should I store it differently?

also sorry about the choice function, was just a skeleton

sorry to all those c lovers, but I'm not really liking c now :(
Last edited on
Just curious, but why did you go from C++ to C?

Side Note: I prefer C++ over C for everything. I'm even switching the C style arrays I used to use to C++ arrays.
My instructor requires to write the application in C. Its funny because C has the poorest capabilities compared to other languages when it comes to string manipulation, from what I've heard.
Last edited on
No problem, you're learning it. By struct of arrays you probably mean an array of struct, right? So, you can make a struct that has as many members as the number of lines that a single input takes. Start with the easy example where your datafile has only one entry, the one you just posted. You need to go over the file line by line and write each into its corresponding "slot" in your struct instance. For example, the first member of your struct holds 4930 new york, the second member holds apartment 101, and so on.

This is a very important practice in my opinion because many applications that use C require you to read a file in a specific way and store it in memory so that you can process it later.

Once you get reading one entry right, you need to think of how to read multiple entries stored in one file. Try it and post your questions if you're having trouble.
Last edited on
****
EDIT: Apparently changing 100 in this line of code
1
2
 
100,                                 // Number of elements in array 


to my actual elements currently initializied in the array (which is 2) works!

I guess I have to add an accumulator each time the user inputs a new slip,call the qsort, and delete 1 each time he/she deletes a slip, and call the qsort again to sort it

Hey I managed to store each line to its right member of my struct. Now I need to sort the array of structs, and I've looked at qsort(). I tried implementing qsort() after following the microsoft example, but to no avail, it outputs a 3 blank lines and a zero two times.

here is my 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct orderSlip
{
	char source[64];
	char destination[64];
	char item[64];	
	int position;
};

typedef int (*compfn)(const void*, const void*);

struct orderSlip data[100];

void decision();
void read();
void moveSlip();
void deleteSlip();
void print();
int  compare(struct orderSlip *, struct orderSlip *);

int main()
{	
	//choice();
	decision();
	//read();
	//pause
	return 0; 
}
void decision()
{
	int choice;
	do
	{
		printf("1 - Add Delivery Slip\n");
		printf("2 - Move Delivery Slip\n");
		printf("3 - Delete Delivery Slip\n");
		printf("4 - View Delivery Slips\n");
		printf("5 - Exit\n");
		printf("Please enter your choice: \n");
		scanf("%i", &choice);
		switch(choice)
		{
			case 1:
				
			read();
			
				break;
			case 2:
			case 3:
			case 4:
				print();
				   qsort((void *) &data,              // Beginning address of array
   100,                                 // Number of elements in array
   sizeof(struct orderSlip),              // Size of each element
   (compfn)compare );                  // Pointer to compare function
   print();
			break;
			default:
				if(choice !=5)
					printf("Input Not Recognized! Please Try Again!\n");
					break;			
		}
	}
	while(choice !=5);
	if(choice == 5)
		printf("Goodbye!");
}


void read()
{
	
	static const char filename[] = "datafile.txt";

	FILE *file = fopen(filename, "r");
	
	if(file != NULL)
	{
		char line[64];
		int n = 0;
		int accumulator = 0;		
		while (fgets(line, sizeof line, file) != NULL)
		{
			
			//remove /n
			for (int i = 0; i < sizeof line; i++ )
			{
    			if ( line[i] == '\n' )
    			{
        			line[i] = '\0';
        			break;
    			}
			}
			//fputs(line, stdout);
			//if first line
			if(accumulator == 0 || accumulator % 4 == 0)
			{
				strcpy(data[n].source, line);
				accumulator++;
			
			}
			//if 2nd line
			else if(accumulator == 1)
			{
				strcpy(data[n].destination, line);
				accumulator++;	
			}
			//if 3rd line
			else if(accumulator % 2 == 0)
			{
				strcpy(data[n].item, line);
				accumulator++;
			}
			//if the line contains only 1 element, must be the
			//position
			else
			{
				int a = line[0] - '0';			
				data[n].position = a;
				accumulator=0;
				n++;
				
			}
			
		};		
		
	fclose(file);
	}
	else
	{
		perror("You goofed!");	
		exit(1);
	}
}

int compare(struct orderSlip *elem1, struct orderSlip *elem2)
{
   if ( elem1->position < elem2->position)
      return -1;

   else if (elem1->position > elem2->position)
      return 1;

   else
      return 0;
}


void print()
{
		printf("%s\n",data[0].source);
		printf("%s\n",data[0].destination);
		printf("%s\n",data[0].item);
		printf("%i\n\n", data[0].position);
		
		printf("%s\n",data[1].source);
		printf("%s\n",data[1].destination);
		printf("%s\n",data[1].item);
		printf("%i\n", data[1].position);
}
Last edited on
Topic archived. No new replies allowed.