Need help with test file.

This is a discussion on Need help with test file. within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program that will check a registration number, and tell if it's valid. To test ...

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    Need help with test file.

    I am trying to make a program that will check a registration number, and tell if it's valid. To test it, I need to input a list of valid, and unvalid numbers, so I can see if it's working properly.

    I know that you can attach a txt file to it, but how do I do it, what is it called, and how should the numbers in the txt file be structured?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    If you have not learned how to read in and out of files you should probably stick to what you know and maybe use a switch statement........
    That is only one suggestion however, and if you would like to learn how to read in and out of files im sure somone might help you.
    For what you are asking a switch statement would work and you should try it with that.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120

    Stillwell, perhaps you can post some code of what you've tried so far or a clearer description of the problem. Your original post is pretty vague.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I know that you can attach a txt file to it, but how do I do it, what is it called, and how should the numbers in the txt file be structured?

    Are you talking about i/o redirection?

    Quote Originally Posted by Thantos
    > redirects standard output
    < redirects standard input
    ?> redirects standard error ( think thats the right symbol)

    Using file redirection for input is a really nice tool for debugging as you should be able to predict the results for the given input and can easily use the same set of input to test until the program works. But if you have a bunch of prompts for the input then your output will be mangled with the error messages.
    But if you are using cerr for error messages and cout for normal messages:
    program.exe <input.txt >output.txt ?>errors.txt
    nicely seperates them.
    You type the stuff at the command line when you execute your program.

    i.e.
    myTestProgram.exe <test.txt
    That way, cin will read from test.txt instead of from the keyboard. You just type the info into your test file as if you were typing it into a console window, and the program should read it all properly.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Hehe, I''m sorry i was so vague, but I don't know how to explain it. I need a txt file that has a list of numbers, both wrong and right ones. I need the progam I am to make, to check these numbers from the .txt file and output if the numbers are valid, or not. How do I make my program aware of the txt file the test numbers are wriiten in? Do I have to include the path of the txt file and how is the txt supposed to be structured?

    I don't know how else to explain it, as I don't know the name of it, but I'm not asking how to make an in/out stream. I'm not asking how to make the program either, I just want to know how to make the program be aware of the txt file

    "You type the stuff at the command line when you execute your program.

    i.e.
    myTestProgram.exe <test.txt"

    I don't understand that. Can't I include a line in the program that includes the txt file automaticly? And if the txt file is just like input from the keyboard, do I need to end every number with a \n?
    Last edited by stillwell; 10-14-2004 at 09:23 AM.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    File I/O

    You should read the tutorials on I/O.....Specifically, Lesson 10

  7. #7
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Thank you. Exactly what I was looking for.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string filename = "\\dir\\subdir\\file.txt";
       int num;
    
       ifstream in(filename.c_str());
       if (!in.is_open())
       {
          cout << "Unable to open file: " << filename << endl;
          return 1;
       }
       in >> num;
       cout << num << endl;
    }

  9. #9
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    I have a problem.

    Code:
    string str;
    
      ofstream a_file ( "one.txt" );
      a_file << "Nintendo is the bestest!!\nLeCock!!";
      a_file.close();
      ifstream b_file ( "one.txt" );
      getline(b_file, str);
      cout << str << "\n";
    This code creates a txt file, inputs the two lines I wrote, and outputs the first one.

    My question is, how do I make it check if there are more lines, and write them out if there are?

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Use a loop. For example:
    Code:
      ifstream b_file ( "one.txt" );
      getline(b_file, str);
      while (!b_file.eof())
      {
        cout << str << "\n";
        getline(b_file, str);
      }
    Notice how I check for eof() immediately after attempting a getline and before I output the string. There are probably better ways, but that would work.

  11. #11
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    That works. Thanks for the help.
    Last edited by stillwell; 10-14-2004 at 05:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21