fprintf not working

I am trying to take text from a file and have it displayed using fprintf at line 46. When I run the below code nothing prints out. My text file has 4 lines with 5 different strings separated by whitespace.

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
#include <cstdlib>
#include <vector>
#include <cstring>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
using namespace std;

//declare my own struct
typedef struct{
	char *str;
	int x;
}Name;

int main(int argc, char **argv){
	//----------Check Command Line Input ------------------------
	//check for valid command line arguments
	if(argc != 2){
		printf("usage: ./userD.txt\n");
		exit(1);
	}

	//open the file
	FILE *fp = fopen(argv[1], "r+");

	//error check file opened correctly
	if(fp == NULL){
		//print useful error message (including errno) and exit gracefully
		perror("file failed to open, ERRNO");
		exit(1);
	}

	//close the file and check for error
	if(fclose(fp) == EOF){
		//print useful error message (including errno) and exit gracefully
		perror("file failed to close, ERRNO");
		exit(1);
	}
	//-------------------------------------------------------------

	char s[100];

	int y;
	Name *n;
	//Get text from file
	fprintf(fp,"%d", 50);




	//vector of pointers to Name structs
	vector <Name *> v;

	//allocate a Name struct
	n = (Name *) malloc(sizeof(Name));



}
1) *print* family prints to something, not reads something
2) You are trying to write into closed file. You closed it on line 34.
So here is what you are actually doing:

you open the file for reading:
1
2
//open the 
FILE *fp = fopen(argv[1], "r+");


then you close the file:
1
2
//close the file and check for 
if(fclose(fp) == EOF)


then try to write to the file
1
2
//Get text from file
fprintf(fp,"%d", 50);


Fix your logic and try again
Sorry I do have the fclose(fp) commented out in my work environment . I was not sure when I was going to close it so I commented it out.
I am trying to make it read out to the terminal. When I run the program I get nothing.
Currently you are writing 50 to the file fp.
If you want 50 to be written on screen, write it on screen:
1
2
3
fprintf(stdout,"%d", 50);
//or simply
printf("%d", 50);
The file I am trying to open has text in it that I need to print to the terminal. I was going to print the first 50 chars to make sure that everything was working. I was thinking that fprintf was the function I needed to use without using c++ puts().
*print* is writing something you provide to something
%d denotes single integer
50 denotes number 50.
In no way this line can mean "read 50 character and output them to screen."

To output something from file you neet to read it first. Say by using fscanf() function:
1
2
3
char buffer [51] = { 0 };
fscanf(fp, "%50c", buffer); //Read 50 characters in buffer
printf("%s", buffer); //Print them to the screen 
Last edited on
I see. Thanks again!
Topic archived. No new replies allowed.