reading and printing a file to screen in C

Reply

Join Date: Aug 2004
Posts: 3
Reputation: sonix is an unknown quantity at this point 
Solved Threads: 0
sonix sonix is offline Offline
Newbie Poster

reading and printing a file to screen in C

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,310
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team 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

 
0
  #2
Aug 23rd, 2004
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6. int r, c, rows, cols;
  7. const char filename[] = "file.txt";
  8. FILE *file = fopen(filename, "r");
  9. if ( file )
  10. {
  11. if ( fscanf(file, "<matrix> rows = %d; ", &rows) == 1 &&
  12. fscanf(file, "cols = %d; ", &cols) == 1 )
  13. {
  14. for ( r = 0; r < rows; ++r )
  15. {
  16. for ( c = 0; c < cols; ++c )
  17. {
  18. double value;
  19. if ( fscanf(file, "%lf", &value) != 1 )
  20. {
  21. break;
  22. }
  23. printf("%g ", value);
  24. }
  25. putchar('\n');
  26. }
  27. }
  28. fclose(file);
  29. }
  30. else
  31. {
  32. perror(filename);
  33. }
  34. return 0;
  35. }
  36.  
  37. /* my output
  38.   1 2
  39.   2 4
  40.   */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 3
Reputation: sonix is an unknown quantity at this point 
Solved Threads: 0
sonix sonix is offline Offline
Newbie Poster

Re: reading and printing a file to screen in C

 
0
  #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 Quick reply to this message  
Join Date: Aug 2004
Posts: 8
Reputation: wewe is an unknown quantity at this point 
Solved Threads: 0
wewe wewe is offline Offline
Newbie Poster

Re: reading and printing a file to screen in C

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

Can anyone help??
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,310
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team 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

 
0
  #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.
  1. /* file.txt
  2.   <matrix>
  3.   rows = 2;
  4.   cols = 2;
  5.  
  6.   3 2
  7.   4 4
  8.   </matrix>
  9.   */
  10.  
  11. #include <iostream>
  12. #include <fstream>
  13. #include <string>
  14.  
  15. int main(void)
  16. {
  17. const std::string filename("file.txt");
  18. std::ifstream file(filename.c_str());
  19. std::string line;
  20. if ( getline(file, line) && line == "<matrix>" )
  21. {
  22. int rows, cols;
  23. if ( file >> line /* "rows" */ &&
  24. file >> line /* "=" */ &&
  25. file >> rows &&
  26. getline(file,line) /* ; */ )
  27. {
  28. if ( file >> line /* "cols" */ &&
  29. file >> line /* "=" */ &&
  30. file >> cols &&
  31. getline(file,line) /* ; */ )
  32. {
  33. for ( int r = 0; r < rows; ++r )
  34. {
  35. for ( int c = 0; c < cols; ++c )
  36. {
  37. double value;
  38. if ( file >> value )
  39. {
  40. std::cout << value << ' ';
  41. }
  42. }
  43. std::cout << std::endl;
  44. }
  45. }
  46. }
  47. }
  48. return 0;
  49. }
  50.  
  51. /* my output
  52.   3 2
  53.   4 4
  54.   */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC