•
•
•
•
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
![]() |
•
•
Join Date: Aug 2004
Posts: 3
Reputation:
Rep Power: 0
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
#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
*/•
•
•
•
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.
/* 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
*/![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- problems with reading random access line from a file (C++)
- Reading numbers from a text file (Java)
- Printing file access permission.... (Shell Scripting)
- printing to file and to stdout (C)
- Reading from external data file (C++)
Other Threads in the C Forum
- Previous Thread: Multithreading problem
- Next Thread: variable function parameters functionname(int a, ...)



Linear Mode