How to read in a file in C.

Started by
17 comments, last by Cromulent 16 years, 4 months ago
Quote:Original post by JavaMava
Quote:Original post by Jan-Lieuwe

Your windows would only disappear if the CPU executes one of those 'exit(1)' statements. Those are executed when your program doesn't find 'input.txt' or when it cannot generate 'output.txt' (the latter being unlikely). Is 'input.txt' in the same folder as your executable?

All C / C++ projects would generate an .exe file, btw. So I don't understand what you're referring to when you say: "Why is it making an .exe as my other programs never have." - unless your previous projects were written using a different language.


I guess it muse be one of the exit commands, I'll go try that. It does find input.txt though as when I run the .exe it takes the text inside the input.txt and displays it on screen. Yes input.txt is in the same folder.

As for the confusion around generating .exe files, that must be my own mistake. We use a text based network, phobos, in my college so I never really see all my files unless I'm specifically looking for them. I guess when I'm at my home PC I just never noticed the .exe.


Ah, what system are you working on? I was sort of assuming you were creating a console application using Visual C++ on a Windows machine. system("pause"); isn't very portable!

If you see the output, then input.txt definitely can be found and the output.txt file can be generated. So it's not executing one of the exit calls then.

Another thing I used to use:

/* include this */#include <conio.h>/* use this to wait for a key */while (!kbhit()){   /* waiting for key... */}


(but I don't think that's part of the ANSI C standard either... getch() is another option)

Edit: just saw your edit. Glad you figured it out!
Advertisement
Quote:Original post by Jan-Lieuwe
Quote:Original post by JavaMava
Quote:Original post by Jan-Lieuwe

Your windows would only disappear if the CPU executes one of those 'exit(1)' statements. Those are executed when your program doesn't find 'input.txt' or when it cannot generate 'output.txt' (the latter being unlikely). Is 'input.txt' in the same folder as your executable?

All C / C++ projects would generate an .exe file, btw. So I don't understand what you're referring to when you say: "Why is it making an .exe as my other programs never have." - unless your previous projects were written using a different language.


I guess it muse be one of the exit commands, I'll go try that. It does find input.txt though as when I run the .exe it takes the text inside the input.txt and displays it on screen. Yes input.txt is in the same folder.

As for the confusion around generating .exe files, that must be my own mistake. We use a text based network, phobos, in my college so I never really see all my files unless I'm specifically looking for them. I guess when I'm at my home PC I just never noticed the .exe.


Ah, what system are you working on? I was sort of assuming you were creating a console application using Visual C++ on a Windows machine. system("pause"); isn't very portable!

If you see the output, then input.txt definitely can be found and the output.txt file can be generated. So it's not executing one of the exit calls then.

Another thing I used to use:

/* include this */
#include <conio.h>

/* use this to wait for a key */
while (!kbhit())
{
/* waiting for key... */
}

(but I don't think that's part of the ANSI C standard either... getch() is another option)


my issue right now is if I run the .exe it works. If I run it from blood shed it displays "Input file not opened". I removed the exit's. So that explains why it wasn't staying up, however what would cause the .exe to find a file that the source code can't.

I'm working on Windows XP, using bloodshed, programming in C, console application.

Quote:Original post by JavaMava
Quote:Original post by Jan-Lieuwe
Quote:Original post by JavaMava
Quote:Original post by Jan-Lieuwe

Your windows would only disappear if the CPU executes one of those 'exit(1)' statements. Those are executed when your program doesn't find 'input.txt' or when it cannot generate 'output.txt' (the latter being unlikely). Is 'input.txt' in the same folder as your executable?

All C / C++ projects would generate an .exe file, btw. So I don't understand what you're referring to when you say: "Why is it making an .exe as my other programs never have." - unless your previous projects were written using a different language.


I guess it muse be one of the exit commands, I'll go try that. It does find input.txt though as when I run the .exe it takes the text inside the input.txt and displays it on screen. Yes input.txt is in the same folder.

As for the confusion around generating .exe files, that must be my own mistake. We use a text based network, phobos, in my college so I never really see all my files unless I'm specifically looking for them. I guess when I'm at my home PC I just never noticed the .exe.


Ah, what system are you working on? I was sort of assuming you were creating a console application using Visual C++ on a Windows machine. system("pause"); isn't very portable!

If you see the output, then input.txt definitely can be found and the output.txt file can be generated. So it's not executing one of the exit calls then.

Another thing I used to use:

/* include this */
#include <conio.h>

/* use this to wait for a key */
while (!kbhit())
{
/* waiting for key... */
}

(but I don't think that's part of the ANSI C standard either... getch() is another option)


my issue right now is if I run the .exe it works. If I run it from blood shed it displays "Input file not opened". I removed the exit's. So that explains why it wasn't staying up, however what would cause the .exe to find a file that the source code can't.

I'm working on Windows XP, using bloodshed, programming in C, console application.


The working directory, when running from bloodshed, is obviously not the directory where your .exe resides in. In Visual C++ you can set the working directory. I don't know bloodshed, so I don't know if it's possible there.

You could hardcode the path into the fopen call to bypass the problem at least, like:

fopen("c:\\projects\\input.txt", "rt");

(this is not good practice though :)
Okay, well... thanks very much for helping me out. I think I've got it down as much as I can right now.

Thank you.
Quote:Original post by JavaMava
Okay, well... thanks very much for helping me out. I think I've got it down as much as I can right now.

Thank you.


Good luck with your assignment ;)
dammit, I thought I had this. Heres my issue. It claims it's finding my file, but when I output it to screen all I get are a bunch of rubbish characters.

#include <stdio.h>main() {/********* STEP 1 - define the files *****/  FILE *in;  char line[81], filename[81] = " ";  printf("Enter a file name: ");  gets(filename);/********* STEP 2 - open the files for processing *****/  in = fopen(filename, "r");  if (in == NULL){    printf("Cannot open %s\n", filename);    }  else {    printf("I found %s\n", filename);    printf("%s", line);  }/********* STEP 4 - close the files *****/  fclose(in);  system("pause");}


the file I'm opening is called "name.txt" Inside it has "Joe Smith" and nothing else. I'm getting the "I found name" text, and then under that it prints out /, a heart, ^, ect... you know rubbish. What am I doing wrong?
You need to use fread(). You are not actually reading anything from the file at the moment.
I think you can read and write file with library CTextFileDocument.

CTextFileDocument lets you write and read text files with different encodings (ASCII, UTF-8, Unicode 16 little/big endian are supported).

You can download free at http://www.codeproject.com/file/textfiledocument.asp
Quote:Original post by The Death
I think you can read and write file with library CTextFileDocument.

CTextFileDocument lets you write and read text files with different encodings (ASCII, UTF-8, Unicode 16 little/big endian are supported).

You can download free at http://www.codeproject.com/file/textfiledocument.asp


I was under the impression that he wanted to use only stdio functions.

This topic is closed to new replies.

Advertisement