CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    15

    Getline needs help

    I have this:file

    A B C D E F
    1 3 5 6 7 8
    2 3 5 3 4 3
    1 8 8 6 3 7
    5 6 9 7 5 2
    0 2 0 0 4 0

    tell me how to read those number into a tabel 5 rows 6 cols..plz
    Thanks

    Cevanne

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    Code:
    #include <cstdio>
    
    int main()
    {
    	char c;
    	FILE *file=fopen("C:\\test.txt", "r");
    	int matrix[5][6], i=0, j=0;
    	if(file)
    	{
    		while(fread(&c, 1, 1, file) && c!='\n');
    		while(fread(&c, 1, 1, file))
    		{
    			if(c>='0' && c<='9')
    				matrix[i][j++]=c-'0';
    			if(c=='\n') 
    				++i, j=0;
    		}
    		fclose(file);
    	}
    	for(i=0;i<5;++i)
    	{
    		for(j=0;j<6;++j)
    		{
    			printf("%d\t", matrix[i][j]);
    		}
    		printf("%c", '\n');
    	}
    }

  3. #3
    Join Date
    Aug 2003
    Posts
    938
    another way to do this , :
    #include <fstream>
    //creat an object:
    ifstream if;
    int matrix[5][6];
    if.open("test.txt");
    for(int i=-0;i<5;i++)
    {
    for(int j=0;j<6;j++)
    {
    if>>matrix[i][j];
    }
    }
    of.close();

    i am not sure of this one, but i think this workes....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured