Pointers and Dynamic Memory Allocation

Sep 11, 2011 at 7:26pm
I have to do this in c programming and not C++.
Last edited on Sep 12, 2011 at 5:23pm
Sep 11, 2011 at 7:33pm
I've always hated when people say "I have to do this in C not C++", at this level what's the difference? I don't mean to go off topic, I need to know this in order to help you.
Sep 11, 2011 at 7:38pm
while I can't read the file using fstream like you do in C++. I learned C++ and not C and there are some difference so that is what I am confused about is C programming.
Sep 11, 2011 at 7:47pm
Oh, so this is just a restriction of the libraries you can use, I think I understand now.

The first issue I see is on Line 21, "fscanf()" is a function and it requires 3 arguments, the first is the input stream, in your case infile, the second is the char format and the third is the "additional arguments" which is basically the variable you are saving this to. http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/
Sep 11, 2011 at 8:12pm
Also, I assume that "line[]" is the array to hold the numbers you are reading in, you have this declared as an char array when it should be an int array, this will allow you to store integers with more then one character like "10".

The inside of your for loop should be where the values are read into your "line" array and sorted. You need to bracket your for loop as well.

EDIT: I almost forgot to mention that since you need to sort the numbers you should have an embedded for loop inside the one that starts on Line 21. This is to read through the integers already gathered and compare them to the number that you just read.
Last edited on Sep 11, 2011 at 8:16pm
Sep 11, 2011 at 8:21pm


I am have trouble counting up integer that is in the file. the while i have wrote dosent seem to give me the correct count.
Last edited on Sep 12, 2011 at 5:21pm
Sep 11, 2011 at 8:40pm
your program must not include any square brackets (‘[‘ or ‘]’) anywhere in the code.
Well, this is a stupid restriction. The offset operator ([]) is just syntactic sugar. x[y] == *(x+y) whenever one is a pointer and the other an integer.
It's like saying that the program can't use for loops. Only while loops.
Last edited on Sep 11, 2011 at 8:41pm
Sep 11, 2011 at 8:42pm
The most frustrating part of this assignment is requirement 'b'. You can access elements of a dynamically allocated array by using brackets, not doing so is just an excersize in frustration and only teaches you the wrong way of doing this.

This is how to dynamically allocate an array in C:
char* infilename = malloc(sizeof(char) * MAXCHAR);
The same will be for your integer array, except you'll want to use the size of it by reading the elements in it first.

EDIT: Damn, ninja'd. Since it's a popular belief that this is stupid, do you have time to ask a TA or the instructor about this particular rule?
Last edited on Sep 11, 2011 at 8:57pm
Sep 11, 2011 at 8:43pm
closed account (D80DSL3A)
You are counting characters instead of integers. A 3 digit integer would be counted as 3 characters (for example).
Try reading integer values from the file instead.
Sep 11, 2011 at 10:48pm
This is almost what you are looking for:

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAR 256

int comp(const void *elem1, const void *elem2){
	return (*(int *)(elem1) - *(int *)(elem2));
}

int main(){
	FILE *file = NULL;
	char ifilename[MAX_CHAR];
	char ofilename[MAX_CHAR];
	int *list;
	int count;

	printf("Input file name:\n>");
	scanf("%s", ifilename);

	file = fopen(ifilename, "r");
	if(!file){
		printf("ERROR: file %s can not be opened!\n", ifilename);
		return 1;
	}

	for(count = 0; !feof(file); ++count){
		int dummy;
		fscanf(file, "%i", &dummy);
	}

	list = new int[--count];
	fseek(file, 0, SEEK_SET);
	for(int i = 0; i < count; ++i){
		fscanf(file, "%i", list + i);
		printf("%i\n", *(list + i));
	}

	fclose(file);
	qsort(list, count, sizeof(int), comp);

	strncpy(ofilename, ifilename, strlen(ifilename) - 4);
	ofilename[strlen(ifilename) - 4] = '\0';
	strcat(ofilename, "_out.txt");

	file = fopen(ofilename, "w");
	if(!file){
		printf("ERROR: file %s can not be opened!\n", ofilename);
		return 1;
	}

	for(int i = 0; i < count; ++i)
		fprintf(file, "%i\n", *(list + i));


	fclose(file);
	return 0;
}
Topic archived. No new replies allowed.