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.
|
|
|
|
[C Language] Opening a text file.
Page 1 of 1
[C Language] Opening a text file.#1Posted 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. #3Posted 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 #4Posted 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.
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. #7Posted 24 February 2007 - 07:56 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? 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.
Page 1 of 1
Fast Reply
1 User(s) are reading this topic
|
||||||||
Forum Index:
Programming Help |
C and C++ |
VB6 |
Java |
VB.NET |
C# |
ASP.NET |
PHP |
ColdFusion |
Perl and Python |
Ruby |
Databases |
Other Languages |
Game Programming |
Mobile Development |
Software Development |
Computer Science |
Industry News |
52 Weeks Of Code |
Programming Tutorials |
C++ Tutorials |
Visual Basic Tutorials |
Java Tutorials |
VB.NET Tutorials |
C# Tutorials |
Linux Tutorials |
PHP Tutorials |
ColdFusion Tutorials |
Windows Tutorials |
HTML/JavaScript Tutorials |
CSS Tutorials |
Flash Tutorials |
Web Promotion Tutorials |
Graphic Design & Photoshop Tutorials |
Software Development Tutorials |
Database Tutorials |
Other Language Tutorials |
ASP.NET Tutorials |
Game Programming Tutorials |
WPF & Silverlight Tutorials |
Mobile Development Tutorials |
Python Tutorials |
Ruby Tutorials |
