Computing.Net > Forums > Programming > Simple text file I/O

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Simple text file I/O

Reply to Message Icon

Name: Jack-R
Date: December 14, 2002 at 16:32:49 Pacific
OS: win98
CPU/Ram: P3
Comment:

I have a pretty simple question about reading and writing from a text file. I don't even know if I should be attempting this just yet, as I am really not familiar with pointers(although I'm not sure if they have anything to do with what I want to do).
I want this program to input a simple string from a user and write it to a file. I will stick with just writing to the file for now, and perhaps later in the thread, to reading from a file.
I am going to outline what I want to accomplish in psuedo-code. If someone could "translate" this to C/C++ or Python, I would appreciate it. Also, feel free to offer any other advice on this. Thanks.

OPEN (something.txt)
PRINT "Enter some text"
INPUT x
WRITE x(something.txt)
CLOSE (something.txt)
END



Sponsored Link
Ads by Google

Response Number 1
Name: Ronin1
Date: December 14, 2002 at 18:44:49 Pacific
Reply:

char string[80];
char buffer[80];

ifstream inFile;
ofstream outFile;

cout [[ "Enter a string";
cin.getline(string, 80, '\n');

outFile.open("somefile.ext", ios::out);

error check

outFile [[ string;

inFile.open("somefile.ext", ios::in);

error check

inFile.getline(buffer, 80, '\n');

cout [[ buffer;

piece of cake ;-)


0

Response Number 2
Name: Jack-R
Date: December 14, 2002 at 22:39:49 Pacific
Reply:

Thanks Roninl. But I am not familiar with most of that code. I obviously have a bit more to learn. Also, I assume from the dot notation that inFile and outFile are objects. Right? How would one do this without working with objects? How about in just regular C as opposed to C++?


0

Response Number 3
Name: Ronin1
Date: December 15, 2002 at 09:53:04 Pacific
Reply:

NP

I'm not an expert in OOP, but I presume ofstream and ifstream are simply objects in the fstream class.

C file i/o is a little more involved and I often have to think about it before I try it so this is likely not the best route, but I'll give it a shot. That's why I tend to use C++ and C :P

char string[80];
char buffer[80];

FILE *fptr;

printf("Enter a string: ");
fgets(string, 80, stdin);

if((fptr = fopen("somefile.ext", "w")) == NULL)
{
printf("unable to write to file.");
return -1;
}

fprintf(fptr, "%s", string);
fclose(fptr);

if((fptr = fopen("somefile.ext", "r")) == NULL)
{
printf("Unable to read from file.");
return -1;
}

fgets(buffer, 80, fptr);
fclose(fptr);

printf("your string: %s", buffer);

Something like that. Look at your compiler documentation or help files for file i/o

HTH


0

Response Number 4
Name: Jack-R
Date: December 16, 2002 at 11:31:21 Pacific
Reply:

Thanks again for the reply. Now I can plainly see I am not ready for this yet. I have some more reading to do.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


VB6 Textbox help a generator app



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Simple text file I/O

C File I/O very important please he www.computing.net/answers/programming/c-file-io-very-important-please-he/7820.html

Java File I/O www.computing.net/answers/programming/java-file-io/12360.html

c++ file I/O, open/close file www.computing.net/answers/programming/c-file-io-openclose-file/11306.html