Scan Textfiles

This is a discussion on Scan Textfiles within the C++ Programming forums, part of the General Programming Boards category; I have a code that is reading lines from a .txt file. In this case ABC.txt. (This is a CLR ...

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

    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 04: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
    383
    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 06: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
    383
    Sounds wonderful. Thank you.. I will experiment with this.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    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 07:37 PM.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Yes, e.g.,
    Code:
    #define statement \
    if (Time >= 1652)
    However, avoid macros.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    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
    383
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    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,185
    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
    383
    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,185
    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.

Page 1 of 2 12 LastLast
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, 05:05 PM

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