What is wrong with this code.

Its purpose is to read the contents of p6.txt and print them on screen. However, nothing prints when compiled. If you can tell me what I'm doing wrong, I'll greatly appreciate it.

Thanx in advance
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
// Proj 6.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>


int _tmain(int argc, _TCHAR* argv[])
{
	int id, number, balance;
	char name[30];

	FILE *textFile = fopen ("I:\\Documents\\p6.txt" , "r");

	if(textFile== NULL) 
		perror ("Error opening file");
	else{
		printf ("\n\n-------------\nFile Opened\n------------\n\n");
		printf ("%-10s%-10s%-10s%-10s", "ID", "NAME", "NUMBER", "BALANCE");
		fscanf (textFile, "%d%s%d%d",&id, name, &number, &balance);

		while (!feof){
			printf ("%-10d%-10f%-10d%-10d\n", id, name, number, balance);
			fscanf (textFile, "%d%s%d%d",&id, name, number, &balance);
		}

		fclose (textFile);
		printf ("\n\n\n\n-------------\nFile Closed\n-------------\n");
	}


	return 0;
}
feof is a function. You are not calling the function; you are checking the pointer to the function against zero.
(your check will always be false, since !non-zero is always zero, or false).

!feof

What's that? You must write !feof(textFile).

BTW, to not repeat some piece of code twice you may write

1
2
3
4
5
6
7
else
{
  while(fscanf (textFile, "%d%s%d%d",&id, name, number, &balance) == 4)
  {
     printf ("%-10d%-10f%-10d%-10d\n", id, name, number, balance)
  }
}
Now, I want it to print only the 4th value stored at name and all its corresponding information. I am trying to learn pointers and am gettin lost, somewhere in the middle.

From what I understand I would have to store them in an array.
Last edited on
Just count the number of lines you've read and when you read the 4th one print it.
I do apologize for all the questions, but can i use getline() to count the lines?
Topic archived. No new replies allowed.