Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,403 C++ Programmers. There are 1,506 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

[C Language] Opening a text file.

 
Reply to this topicStart new topic

[C Language] Opening a text file.

bestbat
post 24 Feb, 2007 - 12:16 PM
Post #1


New D.I.C Head

*
Joined: 14 Feb, 2007
Posts: 32



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
Go to the top of the page
+Quote Post


Craige
post 24 Feb, 2007 - 12:51 PM
Post #2


D.I.C Head

**
Joined: 2 Nov, 2006
Posts: 63

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
Go to the top of the page
+Quote Post

horace
post 24 Feb, 2007 - 01:10 PM
Post #3


D.I.C Addict

Group Icon
Joined: 25 Oct, 2006
Posts: 573

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
Go to the top of the page
+Quote Post

IainMackay85
post 24 Feb, 2007 - 01:11 PM
Post #4


New D.I.C Head

*
Joined: 7 May, 2005
Posts: 44

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
Go to the top of the page
+Quote Post

jayman9
post 24 Feb, 2007 - 01:11 PM
Post #5


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 5,566

You can find a tutorial here that will explain all the necessary steps.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

bestbat
post 24 Feb, 2007 - 01:45 PM
Post #6


New D.I.C Head

*
Joined: 14 Feb, 2007
Posts: 32

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
Go to the top of the page
+Quote Post

Amadeus
post 24 Feb, 2007 - 08:56 PM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,385

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
Go to the top of the page
+Quote Post

bestbat
post 25 Feb, 2007 - 07:46 PM
Post #8


New D.I.C Head

*
Joined: 14 Feb, 2007
Posts: 32

Thanks everyone, I now have a better understanding of opening and closing files.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
Time is now: 5/17/08 07:26AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month