| | |
reading and printing a file to screen in C
![]() |
•
•
Join Date: Aug 2004
Posts: 3
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
#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 */
"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
•
•
•
•
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.
C Syntax (Toggle Plain Text)
/* 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 */
"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
![]() |
Similar Threads
- Reading binary file(image) (C)
- Help reading data from a file (C++)
- specify line of file to start reading? (C++)
- c++ reading text file (C++)
Other Threads in the C Forum
- Previous Thread: Multithreading problem
- Next Thread: variable function parameters functionname(int a, ...)
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency function givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






