Thread: Scan Textfiles

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Scan Textfiles

    I have a code that is reading lines from a .txt file. In this case ABC.txt.
    (This is a CLR Console Application)
    The lines in the file is:

    12/01/1999,1652,21.00
    12/01/1999,1653,21.00

    Now I have a question in mind here. The code is working great but I wonder for the line:
    Code:
    if (Time > 1652)
    Instead of writing this line here. Is it possible to #include a Headerfile from where this line is red instead. To refer to for ex a statement in the included file ?
    If it is possible, will this be a slower process then writing it in the code directly.


    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    int main () 
    { 
    	std::string Date;
    	int Time = 0;
    	char Comma;
    	double Grad = 0;
    		
    	ofstream Main;
      
    	Main.open ("MainFile.txt");
    	ifstream myfile ("ABC.txt");
    
    
    	while	(getline(myfile, Date, ','))           		
    	{
    		myfile >> Time;		      
    		myfile >> Comma;                   
    		myfile >> Grad;				
    		myfile.get();						// read in trailing newline character
    		
    
    		if (Time > 1652)
    
    		{
    		Main << Time <<"\n";    
    		}	
    
    	}								// End of While Statement
    		
    	return 0;	
    
    }
    Last edited by Coding; 02-05-2008 at 05:55 PM.

  2. #2
    coder
    Join Date
    Feb 2008
    Posts
    127
    May you try to explain in other way your purpose?
    I'm not sure I understand what you need.

    You can use #define
    Code:
    #define STATEMENT if (Time > 1652)
    
    // e.g
    STATEMENT {
    	Main << Time <<"\n";    
    }
    so the compiler will substitute "STATEMENT" with that code everytime you use it.

    Isn't this what you meant?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes in a way. This would be my mainid&#233;a as you described that instead of writing:
    if (Time > 1652), I could replace this with STATEMENT.

    As you did, you #define(d) it.

    The thing I am after here is to in any way use a headerfile. I have never done this before. As what I understand this is another file like "headerfile.h" I believe.

    So the line, let us say:
    Code:
    #define STATEMENT if (Time > 1652)
    This line I will write is in another file that I will #include and then refer to STATEMENT in that file if you follow.
    Is this possible to do ?
    Last edited by Coding; 02-05-2008 at 07:31 PM.

  4. #4
    coder
    Join Date
    Feb 2008
    Posts
    127
    It's possible and quite easy:
    just include a header you create
    Code:
    #include "myheader.h"
    and put the #define into it.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Sounds wonderful. Thank you.. I will experiment with this.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    This worked good. One thing I wonder though is as I noticed.
    If you write this it work:

    Code:
    #define statement if (Time >= 1652)
    If you instead write:

    Code:
    #define statement 
    
    if (Time >= 1652)
    This will not work as it isn&#180;t on the same line as #define. Is it possible in any way to make it work to write things under this #define as I tried as I will write very long statements here and if not to use any other approch instead of #define to make it work ?
    Last edited by Coding; 02-05-2008 at 08:37 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Coding View Post
    to make it work to write things under this #define as I tried as I will write very long statements here
    You mean how to write a multiline macros?
    Are you really programming C++? It seems to me a C approach.

    Anyway

    Code:
    #define a "start "  \
    "middle " \
    " and the end of macro"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, e.g.,
    Code:
    #define statement \
    if (Time >= 1652)
    However, avoid macros.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by laserlight
    However, avoid macros.
    ... they make bugs very hard to find in a complex program

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Two things.
    First, when I see this:
    Code:
    		if (Time > 1652)
    
    		{
    		Main << Time <<"\n";    
    		}
    I'm thinking you might need some indentation insight.
    It might be fine for your code right now, but when you start writing more complex code and bigger project, you're going to get in trouble. So I suggest you read that link and understand it.

    And 2)
    You say CLR Console Project. But CLR is .NET and I see no .NET in your code. You're sure it's not just Win32 Console Project?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes it was a little indentation mistake, you are right. I have actually done a CLR Console Application and not a Win32 Project.
    I tried to define something like this but it doesn&#180;t work.

    The thing is that I will write much longer statements than this so I wonder how it could be possible to write it like this under the line #define

    Code:
    #define statement \
    if (Time >= 1652)

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You just continue to add \ to every line you want to break.
    And C++ CLR is just very bad. If you really badly want it, you should be using C# instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Coding View Post
    Yes it was a little indentation mistake, you are right. I have actually done a CLR Console Application and not a Win32 Project.
    I tried to define something like this but it doesn´t work.

    The thing is that I will write much longer statements than this so I wonder how it could be possible to write it like this under the line #define

    Code:
    #define statement \
    if (Time >= 1652)
    You do it just the way you've got it written in your post. If it's not working for you somehow, post some code.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes it worked now. As I will write many lines of code and not want to break it with \ for every line.
    Is this possible to do in any other way.
    Just to give the id&#233;a of what I meen, if I will write 50 lines. So instead of doing 50 of these: \

    Is it possible to enclose all the 50 lines in any other way like brackets of parantheses or any other approach.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    50 lines! What kind of if-statement takes 50 lines?

    Seriously: if it's 50 lines, it's a function -- it may very well be two functions. I can't see the macro approach working well at all here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading textfiles using variable names
    By rjcarmo in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2003, 02:05 PM
  2. scan multiple items in multidimensional array
    By requiem in forum C Programming
    Replies: 1
    Last Post: 04-17-2003, 03:02 PM
  3. while (scan != 'y' or 'n) or if(scan != 'y' or 'n)
    By Blizzarddog in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2002, 01:16 PM
  4. Scan data from file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 07:52 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM