943,745 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 22868
  • C RSS
Aug 21st, 2004
0

reading and printing a file to screen in C

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sonix is offline Offline
3 posts
since Aug 2004
Aug 23rd, 2004
0

Re: reading and printing a file to screen in C

  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.   */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 28th, 2004
0

Re: reading and printing a file to screen in C

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sonix is offline Offline
3 posts
since Aug 2004
Aug 28th, 2004
0

Re: reading and printing a file to screen in C

i need the codes too for readFile in
matrix.txt.. in C++

Can anyone help??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wewe is offline Offline
8 posts
since Aug 2004
Aug 28th, 2004
0

Re: reading and printing a file to screen in C

Quote 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.   */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Multithreading problem
Next Thread in C Forum Timeline: variable function parameters functionname(int a, ...)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC