Putting Data into a 2D array

This is a discussion on Putting Data into a 2D array within the C++ Programming forums, part of the General Programming Boards category; I really went wrong with this one. I have a .txt file with the contents: 1, 2, zz 1, 9, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    32

    Question Putting Data into a 2D array

    I really went wrong with this one.

    I have a .txt file with the contents:

    1, 2, zz
    1, 9, ccc
    2, 3, lll
    2, 13, aaaaa
    3, 3, mmm
    3, 11, oo

    Just so you know the info in the file represents LINE#, COLUM#, text. This .txt file is also the output from another part of this program...but that part works.

    Anyways I will be sorting the data by the alpha characters. I have been looking at example and it seems that I must first put this data into an array so that I can use pointers and then use the qsort() function. I am however stuck as to how to put the data into a 2D array. My code is messed up, what am I doing wrong (besides everything)?
    Code:
    using namespace std;
    
    int main()
    {
    	ifstream inData;
    	ofstream outData;
    	char ch;
    	char lineData[BUFSIZ][50];							
    	int index6 = 0;
    	int lineCount = 1;
    		
    	inData.open("text.txt");										
    	if(!inData)
    		cout << "ERROR in opening " << "text.txt" << endl;
    
    	else
    		if(inData)
    		{
    			cout << "Opening file: " << "text.txt" << endl << endl;
    			ch = inData.get();
    
    			while(ch != EOF)
    			{
    				if(ch == '\n')
    				{
    					lineCount++;
    					index6 = 0;
    				}
    
    
    
    				lineData[lineCount][index6] = ch;
    				ch = inData.get();
    				index6++;
    				cout << lineData << endl;
    			}
    
    
    			outData.close();
    		}
    
    
    
    		return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Here, I'll do what I proboly should have done to begin with...I'll post the program. I suppose it is best to see just what I did. Maybe there is a better way of putting the data into the file for me to retrive it. I'm pretty sure that I have found enough examples on these boards to sort the data with qsort(), but getting the data from the file in a way that it can be sorted is driving me crazy. I belive that I must place it into a 2D array to do this.

    Also I have no clue as to why mode code gets so indented when I post it...sorry if that bugs you.

    Code:
    using namespace std;
    
    #define OPEN	'['
    #define CLOSE   ']'
    
    int main()
    {
    	ifstream inData;
    	ofstream outData;
    	char fileNameIn[50];
    	char fileNameOut[50];
    	char lineOfText[50];
    	char ch;
    	char ch1;
    	char word[50];
    	char lineData[BUFSIZ][50];
    	int bLen = 1;
    	int line;
    	int index;
    	int index1;
    	int index2 = 0;
    	int index3 = 0;
    	int index4 = 0;
    	int index5 = 0;
    	int index6 = 0;
    	int loc[3];
    	int lineCount = 1;
    	int nLineCount = 1;
    	int colum = 0;
    	int openFlag = 0;
    	int closeFlag = 0;
    
    	cout << "Enter filename to read DATA from: ";
    	cin >> fileNameIn;
    	cout << endl;
    	inData.open(fileNameIn);
    	cout << "Enter filename to write DATA to: ";
    	cin >> fileNameOut;
    	cout << endl;
    	outData.open(fileNameOut);
    
    	if(!inData)
    		cout << "ERROR in opening " << fileNameIn << endl;
    
    	else
    		if(inData)
    		{
    			cout << "Opening file: " << fileNameIn << endl << endl;
    			ch = inData.get();
    
    			while(ch != EOF)
    			{
    				if(ch == '\n')
    				{
    					cout << endl;
    					colum = 0;
    					lineCount++;
    
    					for(index4 = 0; index4 < 50; index4++)
    						lineOfText[index4] = ' ';
    
    					index4 = 0;
    				}
    
    				if(isspace(ch))
    				{
    					lineOfText[colum] = putchar(' ');
    					openFlag = 0;
    					closeFlag = 0;
    				}
    				else
    					if(ch == OPEN && openFlag == 0)
    						openFlag = colum + 1;
    
    					else
    						if((ch == CLOSE && closeFlag == 0) && openFlag == 0)
    							closeFlag = 0;
    
    							else
    								if(ch == CLOSE && closeFlag == 0)
    									closeFlag = colum - 1;
    
    								else
    									if(closeFlag != 0)        
    									{
    										for(index1 = openFlag; index1 <= closeFlag; index1++)
    										{
    											word[index2] = lineOfText[index1];
    											index2 = index2 + 1;
    										}
    									
    									
    										word[index2] = NULL;
    										line = lineCount;
    										index = openFlag;
    									
    										loc[0] = line;
    										loc[1] = openFlag;
    										
    										////////////////////////////////////////////
    										//here is where I put the data into the file
    										////////////////////////////////////////////
    									
    										outData << loc[0] << ", " << loc[1] << ", " << word << endl << endl;
    										
    										for(index3 = 0; index3 < strlen(word); index3++)
    											word[index3] = ' ';
    									
    										index3 = 0;
    									
    										for(index3 = 0; index3 < 10; index3++)
    											loc[index] = ' ';
    									
    										index2 = 0;
    										index3 = 0;
    										openFlag = 0;		
    										closeFlag = 0;		
    									}
    
    				lineOfText[colum] = ch;
    				cout << ch;
    				ch = inData.get();
    				colum++;
    			}
    
    			cout << endl << endl;
    		}
    
    		inData.close();
    		outData.close();
    
    /*
    Everything above here works as I want it to. Now I am trying to get the
    data from the new file into a form that I can use to sort it with
    */
    
    	    inData.open(fileNameOut);
    
    		if(!inData)
    			cout << "ERROR in opening " << "text.txt" << endl;
    
    		else
    			if(inData)
    			{
    				cout << "Opening file: " << "text.txt" << endl << endl;
    				ch1 = inData.get();
    
    				while(ch1 != EOF)
    				{
    					if(ch1 == '\n')
    					{
    						nLineCount++;
    						index6 = 0;
    						cout << lineData << endl;
    					}
    
    					lineData[nLineCount][index6] = ch1;
    					ch1 = inData.get();
    					index6++;
    				}
    
    
    			/*
    			This is where I will put the sorting code once I figure out for to
    			correctly read the data from the file in a form that will allow me to sort it.
    			Until I can get the data into a form that I can sort Im just dead in the water.
    			*/
    
    
    				outData.close();
    			}
    
    	return 0;
    }
    Last edited by Zalbik; 11-25-2002 at 01:22 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Also I have no clue as to why mode code gets so indented when I post it
    Your code editor is using real tabs for indentation
    Switch it to using say 4 spaces for each indentation level, then it will look better posted here

    This...
    Code:
            while(ch1 != EOF)
            {
                if(ch1 == '\n')
                {
                    nLineCount++;
                    index6 = 0;
                    cout << lineData << endl;
                }
    
                lineData[nLineCount][index6] = ch1;
                ch1 = inData.get( );
                index6++;
                lineData[nLineCount][index6] = '\0';  //!! you need this!!
            }
    So the aim of this is to get lineData looking something like
    lineData[0] = "1, 2, zz"
    lineData[1] = "1, 9, ccc"

    Though it would seem to me to be a lot easier to read a whole line using this,
    Code:
    inData.getline( lineData[nLineCount] );
    nLineCount++;
    Rather than reading each character at a time.



    Here's a short example showing how to sort such data based on the text field at the end of the line

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    
    // define a type for each line of text
    // it saves more complicated casts later on
    typedef char    line[50];
    
    // find the 2nd comma in a string
    const char *find2ndComma ( const char *p ) {
        for ( int i = 0 ; i < 2 && p != NULL ; i++ ) {
            p = strchr( p, ',' );
            if ( p != NULL ) p++;
        }
        return p;
    }
    
    // compare two strings from the lineData array
    // starting at the 2nd comma in each one
    int compare ( const void *a, const void *b ) {
        const line *pa = (const line*)a;
        const line *pb = (const line*)b;
        const char *comma1 = find2ndComma((const char*)pa);
        const char *comma2 = find2ndComma((const char*)pb);
        return strcmp( comma1, comma2 );
    }
    
    int main ( ) {
        line lineData[6] = {
            "1, 2, zz",
            "1, 9, ccc",
            "2, 3, lll",
            "2, 13, aaaaa",
            "3, 3, mmm",
            "3, 11, oo"
        };
        qsort( lineData, 6, sizeof(line), compare );
        for ( int i = 0 ; i < 6 ; i++ ) {
            cout << lineData[i] << endl;
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    I have never done a typedef before. I'll try it out. It seems much easier to do it that way. Thanks.
    Last edited by Zalbik; 11-26-2002 at 12:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 08:07 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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