C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2009, 04:21 PM   #1
Registered User
 
Join Date: May 2009
Posts: 4
Question Help with user file I/O

Hello,

I am teaching myself C, and am having difficulty writing to a user-specified file. In the code below, fopen keeps returning a null file pointer. When the file location is physically written in the code, however, the program works as expected. My question has two parts:

1) Why does fopen return a null pointer?
2) What should I change to make the program accept user-specified input files?

I have not been able to find an answer to these questions in my C book or through online searches.

Note: The code is written and compiled in Microsoft Visual C++ 2008, if that makes a difference.
Code:
#define _CRT_SECURE_NO_WARNINGS
#define MAX_FILE 100
#define MAX_TEXT 50

#include <stdio.h>

int main()
{
	char file_location[MAX_FILE];
	char text[MAX_TEXT];
	FILE *myfile;
	
	printf("Enter file location: ");
	fgets(file_location, MAX_FILE, stdin);
	myfile = fopen(file_location,"w");
	//myfile = fopen("c:/c test files/test.txt","w"); //PROGRAM WORKS IN THIS CASE

	if (myfile != NULL)
	{
		printf("Enter your text: ");
		fgets(text, MAX_TEXT, stdin);
		fprintf(myfile, text);
	}
	else
	{
		printf("INVALID FILE PATH.\n");
		return 0;
	}
	
	fclose(myfile);

	return 0;
}
Thank you for any assistance.
amac is offline   Reply With Quote
Old 05-10-2009, 04:40 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
Remove the newline from your buffer after you call fgets on it.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 05-10-2009, 07:23 PM   #3
Registered User
 
Join Date: May 2009
Posts: 4
Thank you, quzah.

For anybody's reference, here is the revised (and working!) code:
Code:
#define _CRT_SECURE_NO_WARNINGS
#define MAX_FILE 100 //maximum length of file_location string, including termination character
#define MAX_TEXT 50 //maximum length of text string, including termination character

#include <stdio.h>

int main()
{
	char file_location[MAX_FILE];
	char text[MAX_TEXT];
	int i; //loop counter
	FILE *myfile;
	
	printf("Enter file location: "); //note: fopen doesn't like creating new directories
	fgets(file_location, MAX_FILE, stdin);
	
	for (i=0; i<MAX_FILE; i++) //replace newline character with string termination character
	{
		if (file_location[i] == '\n'){
			file_location[i] = '\0';
		}
	}

	printf("File location: %s\n",file_location);
	myfile=fopen(file_location,"w");

	if (myfile != NULL) //checks if file is valid
	{
		printf("Enter your text: ");
		fgets(text, MAX_TEXT, stdin);
		fprintf(myfile, text);
	}
	else
	{
		printf("INVALID FILE PATH.\n");
		return 0;
	}
	
	fclose(myfile);

	return 0;
}
amac is offline   Reply With Quote
Old 05-11-2009, 02:06 AM   #4
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
1. your loop for replacing \n with \0 could be exitted as soon as the \n or \0 char is encountered - no sence to continue
2. using user provided string as a format for printf is a big security hole. - You should replace fprintf with fputs or use "%s" format in fprintf
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Reply

Tags
file i/o, fopen, null pointer, string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A development process Noir C Programming 30 10-28-2009 04:24 AM
Formatting the contents of a text file dagorsul C++ Programming 2 04-29-2008 12:36 PM
Inventory records jsbeckton C Programming 23 06-28-2007 04:14 AM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM


All times are GMT -6. The time now is 03:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22