reading from a file, easy question?

This is a seemingly trivial code that's been giving me trouble. I'm trying to read some integers from a file. The content of the file is:

1 2 3 4 5 435 23
234 324 34 234 34 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <omp.h>

int main(int argc, char *argv[]) 
{
	FILE *myFile;
	myFile = fopen("myFile.txt", "r");

	//read file into array
	//int numberArray[16];
	//short *A = malloc(sizeof(short) * (10));
	short test;
	int i;

	for (i = 0; i < 2; i++)
	{
		fscanf(myFile, "%d", test);
	}

}


This results in a segmentation fault. Any help would be appreciated.

The parameter must be a pointer to the variable test. Since it is a short integer, you need h to indicate the length.

fscanf(myFile, "%hd", &test);

http://www.cplusplus.com/reference/cstdio/fscanf/
Last edited on
Thank you ^^
Topic archived. No new replies allowed.