User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,234 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,754 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 6783 | Replies: 4
Reply
Join Date: Aug 2004
Posts: 3
Reputation: sonix is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sonix sonix is offline Offline
Newbie Poster

reading and printing a file to screen in C

  #1  
Aug 21st, 2004
anyone remembers the syntax of reading a file into C and subsequently printing it out?

for eg. i have the file 'matrix.txt'
the contents inside the file is as follows:

<matrix>
rows = 2;
cols = 2;

1 2
2 4
</matrix>


how do i read it into my C program and hence prints to screen this:

1 2
2 4


the matrix should also be able to recognise that the rows and cols take the value of 2.

thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: reading and printing a file to screen in C

  #2  
Aug 23rd, 2004
#include <stdio.h>
  #include <string.h>
  
  int main(void)
  {
     int r, c, rows, cols;
     const char filename[] = "file.txt";
     FILE *file = fopen(filename, "r");
     if ( file )
     {
  	  if ( fscanf(file, "<matrix> rows = %d; ", &rows) == 1 &&
  		   fscanf(file, "cols = %d; ", &cols) == 1 )
  	  {
  		 for ( r = 0; r < rows; ++r )
  		 {
  			for ( c = 0; c < cols; ++c )
  			{
  			   double value;
  			   if ( fscanf(file, "%lf", &value) != 1 )
  			   {
  				  break;
  			   }
  			   printf("%g ", value);
  			}
  			putchar('\n');
  		 }
  	  }
  	  fclose(file);
     }
     else
     {
  	  perror(filename);
     }
     return 0;
  }
  
  /* my output
  1 2
  2 4
  */
Reply With Quote  
Join Date: Aug 2004
Posts: 3
Reputation: sonix is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sonix sonix is offline Offline
Newbie Poster

Re: reading and printing a file to screen in C

  #3  
Aug 28th, 2004
thanks so much. i understand all that have been suggested.

how about doing it in c++ syntax?
i am rather new to c++, and thus cant really convert the syntax in c to c++.

hope that someone can help me out.
Reply With Quote  
Join Date: Aug 2004
Posts: 8
Reputation: wewe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wewe wewe is offline Offline
Newbie Poster

Re: reading and printing a file to screen in C

  #4  
Aug 28th, 2004
i need the codes too for readFile in
matrix.txt.. in C++

Can anyone help??
Reply With Quote  
Join Date: Apr 2004
Posts: 3,755
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: reading and printing a file to screen in C

  #5  
Aug 28th, 2004
Originally Posted by sonix
thanks so much. i understand all that have been suggested.

how about doing it in c++ syntax?
i am rather new to c++, and thus cant really convert the syntax in c to c++.

hope that someone can help me out.
It would be better for you to make an attempt first, but in order to kill this in your cross-posting, here is one way.
/* file.txt
  <matrix>
  rows = 2;
  cols = 2;
  
  3 2
  4 4
  </matrix> 
  */
  
  #include <iostream>
  #include <fstream>
  #include <string>
  
  int main(void)
  {
     const std::string filename("file.txt");
     std::ifstream file(filename.c_str());
     std::string line;
     if ( getline(file, line) && line == "<matrix>" )
     {
  	  int rows, cols;
  	  if ( file >> line /* "rows" */ && 
  		   file >> line /* "=" */ &&
  		   file >> rows &&
  		   getline(file,line) /* ; */ )
  	  {
  		 if ( file >> line /* "cols" */ && 
  			  file >> line /* "=" */ &&
  			  file >> cols &&
  			  getline(file,line) /* ; */ )
  		 {
  			for ( int r = 0; r < rows; ++r )
  			{
  			   for ( int c = 0; c < cols; ++c )
  			   {
  				  double value;
  				  if ( file >> value )
  				  {
 					 std::cout << value << ' ';
  				  }
  			   }
  			   std::cout << std::endl;
  			}
  		 }
  	  }
     }
     return 0;
  }
  
  /* my output
  3 2 
  4 4 
  */
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:09 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC