School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,158 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,049 people online right now. Registration is fast and FREE... Join Now!



[C Language] Opening a text file.

[C Language] Opening a text file. Rate Topic: -----

#1 bestbat  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 32
  • Joined: 14-February 07


Dream Kudos: 0

Posted 24 February 2007 - 11:16 AM

I have done a search for this, most of the results were for C++ and I gave up after 6 pages.

I'm trying to open a text file.

My code is:
#include <stdio.h>


int main (void)
{
fopen(c:\numberarray.txt,r);
printf("File has been opened");
fclose;
printf("File has been closed");
}



It won't compile.
Was This Post Helpful? 0
  • +
  • -


#2 Craige  Icon User is offline

  • D.I.C Head
  • PipPip
  • View blog
  • Group: Members
  • Posts: 63
  • Joined: 02-November 06


Dream Kudos: 0

Posted 24 February 2007 - 11:51 AM

fopen("file", "a");



a can be a range of values, each standing for how to open the file. a is append, r is read. I don't know all the values off the top of my head. You'll have to google for them.
Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • Icon
  • Group: Expert
  • Posts: 1,603
  • Joined: 25-October 06


Dream Kudos: 50

Posted 24 February 2007 - 12:10 PM

in addition to enclosing the file name and mode in " (as indicated by Craige) you need to enter \ as \\ in a string specifing a filename, e.g.
fopen("c:\\numberarray.txt","r");

i.e. in "c:\numberarray.txt" the \n would be the newline character whereas in c:\\numberarray.txt" the \\ is the \ character
Was This Post Helpful? 0
  • +
  • -

#4 IainMackay85  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 48
  • Joined: 07-May 05


Dream Kudos: 0

Posted 24 February 2007 - 12:11 PM

you need to put quote marks round your file name or the compiler won't treat it as a string.
fopen("c:\numberarray.txt",r);



also you haven't declared r

FILE *r



is this the first c program you have done?

#include <stdio.h>


int main (void)
{
FILE *r
fopen("c:\numberarray.txt",r);
printf("File has been opened");
fclose;
printf("File has been closed");
}



its either that or *FILE r i can't remember.


View Postbestbat, on 24 Feb, 2007 - 12:16 PM, said:

I have done a search for this, most of the results were for C++ and I gave up after 6 pages.

I'm trying to open a text file.

My code is:
#include <stdio.h>


int main (void)
{
fopen(c:\numberarray.txt,r);
printf("File has been opened");
fclose;
printf("File has been closed");
}



It won't compile.

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is online

  • Student of Life
  • Icon
  • View blog
  • Group: Admins
  • Posts: 8,836
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Posted 24 February 2007 - 12:11 PM

You can find a tutorial here that will explain all the necessary steps.
Was This Post Helpful? 0
  • +
  • -

#6 bestbat  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 32
  • Joined: 14-February 07


Dream Kudos: 0

Posted 24 February 2007 - 12:45 PM

Thanks all of you.

@ IainMackay85
No this is not my first C program, I didnt think i needed to declare r as i'm using it to tell fopen that I want to read the file.

@jayman thanks for the link, i'll go through it and post back my results.
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • Icon
  • View blog
  • Group: Alumni
  • Posts: 13,376
  • Joined: 12-July 02


Dream Kudos: 25

Posted 24 February 2007 - 07:56 PM

View PostIainMackay85, on 24 Feb, 2007 - 03:11 PM, said:

you need to put quote marks round your file name or the compiler won't treat it as a string.
fopen("c:\numberarray.txt",r);



also you haven't declared r

FILE *r



is this the first c program you have done?

Not sure of the reason for that question, especially given your answer.

One does not need to declare r - it needs to be enclosed in quotes. It is not a pointer to a file, it is a mode that indicates the file is to be read. You are likely thinking of a file pointer that is declared to allow the program to keep rack of the file being accessed, such as
FILE *r;
r = fopen("c:\numberarray.txt","r");


bestbat, you were correct in your assumptions about the mode identifier, it does not need to be declared. It does need to be in quotes, however.
Was This Post Helpful? 0
  • +
  • -

#8 bestbat  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 32
  • Joined: 14-February 07


Dream Kudos: 0

Posted 25 February 2007 - 06:46 PM

Thanks everyone, I now have a better understanding of opening and closing files.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month