Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,197 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,407 people online right now. Registration is fast and FREE... Join Now!




[C Language] Opening a text file.

 
Reply to this topicStart new topic

[C Language] Opening a text file.

bestbat
24 Feb, 2007 - 11:16 AM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
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:
CODE

#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.

User is offlineProfile CardPM
+Quote Post


Craige
RE: [C Language] Opening A Text File.
24 Feb, 2007 - 11:51 AM
Post #2

D.I.C Head
**

Joined: 2 Nov, 2006
Posts: 63


My Contributions
CODE

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.
User is offlineProfile CardPM
+Quote Post

horace
RE: [C Language] Opening A Text File.
24 Feb, 2007 - 12:10 PM
Post #3

D.I.C Lover
Group Icon

Joined: 25 Oct, 2006
Posts: 1,098



Thanked: 94 times
Dream Kudos: 50
My Contributions
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.
CODE

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
User is offlineProfile CardPM
+Quote Post

IainMackay85
RE: [C Language] Opening A Text File.
24 Feb, 2007 - 12:11 PM
Post #4

New D.I.C Head
*

Joined: 7 May, 2005
Posts: 44


My Contributions
you need to put quote marks round your file name or the compiler won't treat it as a string.
CODE

fopen("c:\numberarray.txt",r);


also you haven't declared r

CODE

FILE *r


is this the first c program you have done?

CODE

#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.


QUOTE(bestbat @ 24 Feb, 2007 - 12:16 PM) *

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:
CODE

#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.


User is offlineProfile CardPM
+Quote Post

Jayman
RE: [C Language] Opening A Text File.
24 Feb, 2007 - 12:11 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,062



Thanked: 159 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You can find a tutorial here that will explain all the necessary steps.

User is offlineProfile CardPM
+Quote Post

bestbat
RE: [C Language] Opening A Text File.
24 Feb, 2007 - 12:45 PM
Post #6

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: [C Language] Opening A Text File.
24 Feb, 2007 - 07:56 PM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,976



Thanked: 116 times
Dream Kudos: 25
My Contributions
QUOTE(IainMackay85 @ 24 Feb, 2007 - 03:11 PM) *

you need to put quote marks round your file name or the compiler won't treat it as a string.
CODE

fopen("c:\numberarray.txt",r);


also you haven't declared r

CODE

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
CODE

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.
User is offlineProfile CardPM
+Quote Post

bestbat
RE: [C Language] Opening A Text File.
25 Feb, 2007 - 06:46 PM
Post #8

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
Thanks everyone, I now have a better understanding of opening and closing files.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:02AM

Live C++ Help!

Be Social

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

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month