7 Replies - 253 Views - Last Post: 10 March 2011 - 02:45 PM Rate Topic: -----

#1 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Opening a file.

Posted 10 March 2011 - 02:02 PM

printf("What file: ");
fgets(textfile, 500, stdin);



I then try

fopen(textfile, "a+");



I always get a null pointer returned. I have tried declaring textfile as a character pointer and an array of characters.

I was told not to use scanf so I tried using fgets. So confused. This is C btw not C++.

This post has been edited by Matty919: 10 March 2011 - 02:06 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Opening a file.

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Opening a file.

Posted 10 March 2011 - 02:05 PM

Please show an entire program illustrating the problem.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Opening a file.

Posted 10 March 2011 - 02:08 PM

hmm, can you show us your whole code?
Was This Post Helpful? 0
  • +
  • -

#4 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: Opening a file.

Posted 10 March 2011 - 02:14 PM

Changed it slightly.

#include <stdio.h>

int main()
{
	FILE *fp;
	char fname[501];

	printf("What file: ");
	fgets(fname, 500, stdin);

	fp = fopen(fname, "w+");

	if(fp == NULL)
	{
		printf("error");
	}

	return 0;
}

This post has been edited by Matty919: 10 March 2011 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Opening a file.

Posted 10 March 2011 - 02:27 PM

did you write the file path write? dose your program have permission to open the file?
Was This Post Helpful? 0
  • +
  • -

#6 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: Opening a file.

Posted 10 March 2011 - 02:30 PM

Replacing

fgets(fname, 500, stdin);



with

scanf("%s", &fname);



works. Yep I have permission. Used same file name: C:/test.txt

This post has been edited by Matty919: 10 March 2011 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Opening a file.

Posted 10 March 2011 - 02:31 PM

What was your input?

You may want to print the file name before you try to open the file to insure your program is seeing what you entered properly. Also you may have problems if you are entering file names with spaces.

EDIT:
Also the fgets might leave the '\n' at the end of your string and you may have to manually remove it.


Jim

This post has been edited by jimblumberg: 10 March 2011 - 02:33 PM

Was This Post Helpful? 1
  • +
  • -

#8 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: Opening a file.

Posted 10 March 2011 - 02:45 PM

View Postjimblumberg, on 10 March 2011 - 02:31 PM, said:

EDIT:
Also the fgets might leave the '\n' at the end of your string and you may have to manually remove it.


Jim


Looks like that was it, I hate newline characters I swear they always sneak in just to screw with me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1