A better way?

This is a discussion on A better way? within the C++ Programming forums, part of the General Programming Boards category; This is an old file that I would like to update, I have tried to convert everything over to string ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    45

    A better way?

    This is an old file that I would like to update, I have tried to convert everything over to string but it ended up a failure wasn't sure what functions to use and classes. The buffer flushes fix the naggin getline bug which I have fixed in another prog but for some reason I the warnings came back when I starting working with this one. If anyone has some suggestions on updating this that would be great.

    Code:
    #include "stdafx.h"
    #include "test1.h"
    #include "stdlib.h"
    #include "iostream.h"
    #include "fstream.h"
    #include "string.h"
    #include <io.h>
    #include <string>
    #include <vector>
    #include <stdio.h>
    #include <iomanip.h>
    #include <strstrea.h>
    #include <time.h>
    const int lineSize = 501;
    
    int Annotate ( const char *inSource2)
    /* Searches given file with user entered keycodes and appends
    to the user given file with the extension of .all*/
    
    
    {
    	ifstream fin;
        ofstream fout;
    	char	 	s2Line[lineSize];			// for data lines
        char filename [10];
    	char keyCode [8];//keycode buffer string 
    	int	source2Line = 0;
    	int keynum;
    	char bs[2];//buffer flusher
    	
    	cout << "What's your File Name? ";
    	cin.getline (filename,11);
    	strncat (filename, ".all", 6);
    	cout << "Your File Name is " << filename << ".\n";
    	cout<<"How many Key Codes in this file? ";//Allows for multiple keycodes in same file.
    	cin>>keynum;
    	cin.getline(bs,2);//flushes the buffer
    	
    	while (keynum!=0)
    	{
    	ifstream 	source2(inSource2, ios::in);
    	--keynum;
    	cout << "Enter a Key Code? ";
    	cin.getline (keyCode,9);//sends a keycode no longer than 9 char long to keyCode
    	cout << "Your keycode is " << keyCode << ".\n";
    	fout.open(filename,fstream::in | fstream::out | fstream::app);//opens the .all file in append mode			
    	
    	if (!source2) 
    	{
        	cerr << "Can't open input files\n";//Error protection!
    		return -1;
    	}
    	
    	while (source2.getline(s2Line, lineSize, '\n')) 
    	{
    		char subString[30];
    		strncpy (subString,s2Line,29);
    		
    		if (strstr(subString, keyCode) != 0)//Only searches the first 30 columns 
    		{ 
    		
    		fout<<s2Line<< endl;//Writes the whole line and returns to the next line
    		
    		}
    	}
    	source2.close();//closes the master file
    	fout.close();//closes the new parsed file
    	
    	}
    	
    	
        
    	return 0;
    }
    
    int main (void)
    {
    	char ans[2];//buffer flusher
    	int Groupnum;
    	cout<<"How many files? ";
    	cin>> (Groupnum);
    	cout<<"Your number is "<<Groupnum<<endl<<endl;
    	cin.getline(ans,2);//flushes buffer
    	while (Groupnum !=0)//allows you to enter multiple files 
    	{
    	Annotate("test.all");
    	--Groupnum;
    	}
    	return(0);
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Sure. First, let's get rid of most of those includes:
    Code:
    //These are all you need
    #include <iostream>
    #include <fstream>
    However, you'll need to use a few using directives (unless you want to go to each cout, cin, endl, etc. and put a std:: in front of them.
    Code:
    using std::ifstream;
    using std::ofstream;
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fstream;
    using std::ios;
    using std::cerr;
    There are a few places where you can use a string instead of a char array. I'll show you an example of one. First, be sure to #include <string> and put a using std::string; in. Then look at this section of code:
    Code:
    int Annotate ( const char *inSource2)
    /* Searches given file with user entered keycodes and appends
    to the user given file with the extension of .all*/
    {
        ifstream fin;
        ofstream fout;
        char s2Line[lineSize];			// for data lines
    //  char filename [10]; //old code
        string filename; //new code
    By making that change, you'll have to change a couple of other lines. For instance, the getline right below it:
    Code:
        cout << "What's your File Name? ";
    //  cin.getline (filename,11); //old code
        getline(cin,filename); //new code
    The string concatenation is also different, but intuitive and easy:
    Code:
    //  strncat (filename, ".all", 6); //old code
       filename = filename + ".all"; //new code
    After those three changes, it should compile again. I'll confess that I didn't try to run it, so there may be some logical errors, but that's the gist of it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    I have done some work with strings before but I don't understand the
    using std:fstream;
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fstream;
    using std::ios;
    using std::cerr;
    it seems when I used it before I add using namespace std or one of my other includes took care of it so why would that be considered better?

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    The using namespace std; command brings the whole std namespace into the global namespace, which can lead to some ugly name collisions. That's not really a problem in smaller projects, but it can be in larger ones. Besides, it's like using a chain saw to carve out toothpicks. You might get it done, but it's easier to use something smaller.

    The using std:: command only brings that particular command into the global namespace, so it's a whole lot easier to avoid name collisions.

    If I'm coding something up in a hurry (read: sloppily), then I use using namespace std;. Otherwise, I use the using directive to bring in only the parts that I need.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

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