Hi, i'm a student and i needed help in storing all my information from C to a text file. Could anyone help me out.
Thank you

Recommended Answers

All 9 Replies

This is the C forum. Do you believe that we don't deal with C problems? ;)

One look around the place should have enlightened you on that point. Post your code, and describe what the problem is - tell us what it is that has you stumped.

Hi, i'm actually using a switch statement at the same time my codes are all working so far there is no problem.
But i wanted all my data i entered to be stored in a text file.
I have used this code, and it does not show me any error but, "is it really getting saved in a text file and how can i view it?"
Are the below codes correct? if not please help me, Thanks...

below are the codes shown, which i used to store my data into a text file...

/*codes for storing data in a textfile*/

	FILE *ofp; 

	ofp = fopen("myprogram.txt", "w");//creating a text file.//fprintf(ofp, "%d \n %f", i, f);
	

	fprintf(ofp,"CLIENT REGISTRATION DETAILS\n\n");

	
	fprintf(ofp,"Client_RegNumber:%s\n\n",Client_RegNumber);

	
	fprintf(ofp,"Client_FirstName:%s\n\n",Client_FirstName);

	
	fprintf(ofp,"Client_LastName:%s\n\n",Client_LastName);

	
	fprintf(ofp,"Location:%s\n\n",Location);


	fprintf(ofp,"PhoneNumber:%s\n\n",PhoneNumber);

	}
		
	return(0);
}

@OP

>>"is it really getting saved in a text file and how can i view it?"

Open the file :)
Since you have not specified the path explicitly, it generally gets created in the same directory as the source file or sometimes the executable.

Just open it and check it out,

sir, what do you mean by specifying the path, could you give me an example
and i checked the folder it does not have my work saved in it...
thank you

sir, what do you mean by specifying the path, could you give me an example
and i checked the folder it does not have my work saved in it...
thank you

Specifying the path means telling the compiler where you want your File on Disk...

like:
ofp = fopen("D:\\xyz\\myprogram.txt", "w"); // D is Drive Name & xyz is folder name on that drive....

Thankyou...i'll try it and inform if it works...

Hi, tried...but i cant see my data saved onto the drive.
Is it possible to use those codes i posted in a switch statement...
i have posted them at the exact end before the return function, though it does not give me any error is it ok??
would you help me solve this since i have no error at the same time my data is not saved...
thank you

can you post your complete code here.... then it would be easier to understand exactly what's going on....

Your code doesn't show any closure for the file you created. That can cause a problem.

Add:

fclose(ofp);

when your program is done writing data to the file. That will flush out any data that hasn't been written yet to the file, that is being held by a buffer.

My bet is that is your problem. Most hard drives are set up for buffered writing (for efficiency), and you haven't put much data into the file buffer, yet. Then you didn't close the file, so the data may not have been written out.

;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.