Thread: help with text input

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > What about my integer classification question, can anyone answer that?
    > input is of class int
    Your book was just telling you what data type to use for the numerical data.

    Your program will differentiate between whitespace, digits, characters and punctuation if you use the right functions to look for those things. All input to a console program is textual in nature... integers aren't normally sent to the console (except in special cases), so if necessary, you would convert a string to an integer. In a counting program such as this one, it isn't necessary.

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    What about my integer classification question, can anyone answer that?
    Oh, I missed that part. My bad. The reason you have to use an int for fgetc() is because of EOF. fgetc() returns either a character in the range of unsigned char or EOF. unsigned char is guaranteed to be positive, and EOF is guaranteed to be negative, so fgetc() can't return an unsigned char. It also can't return a signed char or a bunch of legitimate characters won't be returned right. So fgetc() returns an int because characters are just small integers. int can hold the full range of an unsigned char and the negative value of EOF.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Gotcha, after looking at closer, divines code doesnt compile. Looking back at it though i dont see a need to structure it like that, because the way noir has it structured makes alot more sense. The only thing i didnt understand was the addition of the "argument stuff" i dont think im familiar with those commands. I probably wont split my program into two different blocks but well see.
    So how do i test this? Do i need to make a .txt file in the same directory?

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The only thing i didnt understand was the addition of the "argument stuff" i dont think im familiar with those commands.
    You can call your program from the dos prompt and pass it arguments. Those arguments can be used from the parameters to main, argc and argv. If you call your program like this:
    Code:
    C:\>prog.exe testfile.txt
    argc will be 2 and argv will be an array of strings that looks like
    Code:
    {"prog.exe","testfile.txt",NULL}
    and you can get to the test file with argv[1]. The best way is to experiment with different ways of calling your program and with different files to see how your OS handles dos arguments, but the easiest way to get it right the first time is to hard code an absolute path to the file you want:
    Code:
    int main( void ) {
      FILE *fp = fopen( "C:\\worker\\testfiles\\test.txt", "r" );
    Do i need to make a .txt file in the same directory?
    If you use a relative path the file probably has to be in the same directory as the program's exe unless you've added the program to a path environment variable, then the current working directory has to be the same directory that the test file is in.

  5. #5
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Quote Originally Posted by Alphawaves View Post
    Gotcha, after looking at closer, divines code doesnt compile.
    it does on gcc-4.1.2

    if it's segfaulting it's because the file specified doesn't exist. if you don't want it to segfault, you need to check if the stream is actually open before reading from it. that or specify a file that actually exists

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Choosing a variable based on user text input.
    By Compiling... in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2005, 01:21 AM
  2. Parsing Text File and gathering input variables
    By azamsharp1 in forum C Programming
    Replies: 2
    Last Post: 10-26-2005, 08:43 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  5. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 03:17 PM