NO

Author Topic: a real beginner  (Read 4668 times)

msrbbrite

  • Guest
a real beginner
« on: October 08, 2012, 01:51:46 AM »
How do I input a file to my program?? I know how to do it with another program but not this one

CommonTater

  • Guest
Re: a real beginner
« Reply #1 on: October 08, 2012, 02:08:26 AM »
Ok, it's not eniterly clear what you mean by "program"...
 
Basically if you know how to open, read, write, and close files in C... it's the same process for every program, althought the contents of the files may be somewhat different.
 
If by chance you are saying "program" when you really mean "language", that's a little different since each programming language handles files a bit differently... Take a look in the Pelles C helpfile at the functions in STDIO.H, most of the functions that start with f are file related.
 
The best thing is that if you've been trying, you can post your problem source code and let us help you sort it out...
 

msrbbrite

  • Guest
Re: a real beginner
« Reply #2 on: October 08, 2012, 11:26:01 AM »
My source code is written to work with a text file input. I usually use puTTY and VIM to write my code. I down loaded Pelles C so I didnt need to be connected to my school server to write my code. It work a little different so I'm not sure when I have the source code on the screen using Pelles C how do I input my file? The source code counts the number of characters in any text file.

CommonTater

  • Guest
Re: a real beginner
« Reply #3 on: October 08, 2012, 01:03:44 PM »
This isn't enough information to give you a good answer...

Did you make a project in Pelles C?
Did you build your project?
How are you opening the text file?

As I said, it would really help if you posted your source code so I can see what's going on...


msrbbrite

  • Guest
Re: a real beginner
« Reply #4 on: October 08, 2012, 01:21:17 PM »
yes, I made the project in Pelles C and built the project... or did I?? I wrote the source code in Pelles C if thats what you mean then compiled it then ran it. Nothing happened though because the file wasn't there. It needs to be input. I guess the question is how do I open the file to use it as an input? Do I actually have to make it a project? My questions may sound silly because I'm really new to programming and Pelles C

CommonTater

  • Guest
Re: a real beginner
« Reply #5 on: October 08, 2012, 02:54:57 PM »
yes, I made the project in Pelles C and built the project... or did I??

If you had done this, you would know...
In POIDE (The editor for Pelles C) creating a project is a distinct step in the process.  You cannot simply open the program and start typing in source code...
 
In the IDE click File -> New -> Project ...  you probably want a Win32 Console Program, from the empty projects section, for this... give your project a name at the bottom of the dialog and click OK.
 
That leaves you with an empty project.
 
Now click File -> New -> Source code  (or type CRL+N)
 
The new page is where you type in your source code.
 
When you finish click the save icon on the taskbar.. you will be asked to name your file and then asked if you want to add it to your project.
 
After saving the file you can build it by clicking the Build icon on the taskbar...
 
So, as you can see... if you had done this, you would know :D
 
Also take a look in the help file...
Help -> Contents -> Integrated Environment -> POIDE Integrated Environment -> Your First Project
... for a more detailed explaination.
 
FWIW... Pelles C has one of the best help files I've ever seen... you should get used to using, as you will be in there a lot.
 
Quote
I wrote the source code in Pelles C if thats what you mean then compiled it then ran it.

Did you get any errors during the compile process?
The compiler and linker stop if there are any errors.
 
 
Quote
Nothing happened though because the file wasn't there. It needs to be input.

There could be other reasons why "nothing happened"... especially of you got errors during the build process.
 
Pelles C emits Windows executable files... running a file after a successful compile should produce a console window with (at least) "Press any key to continue..." showing.  If you didn't get that, it's likely your program is not built... either because of errors at compile time or because you haven't created a proper project for it to build.
 
Quote
I guess the question is how do I open the file to use it as an input?

Pelles C is standard C ... So you would open and process the file in the usual C manner, using fopen() fread() fwrite() fclose() etc.  There's nothing special or unique to Pelles C about this... 
 
You need to read up on how each of these functions works... For example; type fopen (or place your editing cursor on it) in your source page and press F1 on your keyboard... The help file will open with a complete explaination of what the function does.
 
Quote
Do I actually have to make it a project?

Yes.  The project is the instructions the compiler and linker use to know how to build your code.
 
Quote
My questions may sound silly because I'm really new to programming and Pelles C

No worries... we've all been there at one time or another. 
 
The biggest problem is that I can't see your screen to help you figure out where it went wrong.  When asking questions on line like this, the more information you give us the better... samples of the failed source code, project files attached to messages, detailed descriptions of the problem, etc. all contribute very strongly to our ability to help you.  We're a pretty smart bunch but I don't think any of us are mind readers. :D
 

msrbbrite

  • Guest
Re: a real beginner
« Reply #6 on: October 08, 2012, 03:40:10 PM »
Well you've been VERY helpful and thorough in your explanation. From your discription yes I did create a project. No I did not receive errors when compiling. I do see where my problem is though. This is the first time I'm using an input file. Up to now everything I've written was a stand alone program without using an input. So that what I need to read up on and get a better understanding of. I will check out the help files in pelles c as well. Thank you

CommonTater

  • Guest
Re: a real beginner
« Reply #7 on: October 08, 2012, 04:04:58 PM »
No worries ... if you have more questions, feel free to ask. 

Opening and reading a file isn't all that difficult...  If you are doing a character counter, you will most likely want to read up on the fopen(), fgetc() and fclose() functions.   Just be sure you pay attention to the returned values from each function and do some error catching, since whenever you deal with externals (files, devices, etc) there is always the risk of things not going as planned.

Something like...
Code: [Select]
FILE* f;
INT chr;
 
f = fopen("c:\\mystuff\\myfile.txt","r")  // note the doubled slashes
 
// is the file open?
if (!f)   
  {
     puts("Could not open the file");
     exit (-1);
  }
 
while(1)
  {
    chr = fgetc(f);  // fetch next character
    if (chr == EOF)  // bail at end of file
      break;
 
    // do what ya gotta do with chr here
  }
 
fclose(f);

Easy stuff!


 
« Last Edit: October 08, 2012, 04:11:54 PM by CommonTater »