*Cole*

I am trying to read from a file using fscanf and it won't read past the first character. I was wondering if anybody could tell me why Here is the code, I pass the file many times to different parts of the program, perhaps I'm doing this wrong

Code Snippet

#include <stdio.h>
#include <cstdlib>
#include "Rational.h"
using namespace std;

//funtions
void getConstraints(int &, int &, FILE *);
void getInput(Rational**, int, int, FILE *);

//global variables
char inFileName[16], outFileName[16];

int main()
{
int rows = 0;
int cols = 0;
cout << "Please enter input file name (max 15 chars):\n";
cin >> inFileName;
FILE *fp;
if((fp=fopen(inFileName,"r"))==NULL)
{
cout<< "Error opening file.";
}
getConstraints(rows, cols, fp);

if(rows != 0 && cols != 0)
{
Rational **a = new Rational *[rows];
for(int k=0; k<rows; k++)
a[k] = new Rational [cols];

getInput(a, rows, cols, fp);

for(int k = 0; k < rows; k++)
delete[] a[k];
delete[] a;
}


return 0;
}

void getConstraints(int &R, int &C, FILE *f)
{

fscanf(f, "%d", &R);
R++;
fscanf(f, "%d", &C);
C = C*2 + 1;
return;
}

void getInput(Rational** a, int R, int C, FILE *f)
{
int row, col;
char c1, c2;
bool negOne = false;
row = R;
col = C;
Rational temp;
col = col/2 - 1;
a[0][0] = 0;
for(int j = 1; j <= col; j++)
{
temp.input(f);
a[0][j] = temp;
}
for(int i = 1; i < row; i++)
{
temp.input(f);
a[i][0] = temp;
fscanf(f, "%c%c ", &c1, &c2);
if(c1 == '<')
negOne = true;
for(int j = 1; j <= col; j++)
{
temp.input(f);
a[i][j] = temp;
}
if(negOne == true)
{
for(int j = 0; j <= col; j++)
{
a[i][j] = a[i][j] * -1;
}
}
negOne = false;
}
return;
}

rational class that I'm using
Code Snippet

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;

//Class for Rational Numbers
class Rational
{
public:
//constructors
Rational();
Rational(int numerator, int denominator);
Rational(int numerator);
void operator=(const Rational&);

//member functions
friend Rational operator+(const Rational&, const Rational&);
friend Rational operator-(const Rational&, const Rational&);
friend Rational operator*(const Rational&, const Rational&);
friend Rational operator/(const Rational&, const Rational&);
friend Rational operator<(const Rational&, const Rational&);
friend Rational operator>(const Rational&, const Rational&);
void reduce();
void input(FILE *);
void output(ostream& out_s);

//accessor functions
int Numerator() {return numerator;}
int Denominator() {return denominator;}

private:
//member variables
int numerator;
int denominator;
};

Rational::Rational() : numerator(0), denominator(1)
{
}

Rational::Rational(int num, int dem)
{
numerator = num;
denominator = dem;
}

Rational::Rational(int num)
{
numerator = num;
denominator = 1;
}

Rational operator+(const Rational& r1, const Rational& r2)
{
Rational r3;
r3.numerator = r1.numerator * r2.denominator - r2.numerator * r1.denominator;
r3.denominator = r1.denominator * r2.denominator;
r3.reduce();
return r3;
}

Rational operator-(const Rational& r1, const Rational& r2)
{
Rational r3;
r3.numerator = r1.numerator * r2.denominator - r2.numerator * r1.denominator;
r3.denominator = r1.denominator * r2.denominator;
r3.reduce();
return r3;
}

Rational operator*(const Rational& r1, const Rational& r2)
{
Rational r3;
r3.numerator = r1.numerator * r2.numerator;
r3.denominator = r1.denominator * r2.denominator;
r3.reduce();
return r3;
}

Rational operator/(const Rational& r1, const Rational& r2)
{
Rational r3;
r3.numerator = r1.numerator * r2.denominator;
r3.denominator = r1.denominator * r2.numerator;
r3.reduce();
return r3;
}

Rational operator<(const Rational& r1, const Rational& r2)
{
if(r1.numerator * r2.denominator < r2.numerator * r1.denominator)
return true;
else
return false;
}

Rational operator>(const Rational& r1, const Rational& r2)
{
if(r1.numerator * r2.denominator > r2.numerator * r1.denominator)
return true;
else
return false;
}
void Rational::reduce()
{
int n = 2;
bool div = true;
bool div2 = false;
while(div == true)
{
//checks if divisible
for(int i = 2; i < numerator || div2 == true; i++)
{
if(numerator % i == 0 && denominator % i == 0)
div2 = true;
}
//if divisible do division
if(div2 == true)
{
while(numerator % n != 0 && denominator %n != 0)
n++;
numerator = numerator/n;
denominator = denominator/n;
}
else
return;
div2 = false;
n = 2;
}
return;
}

void Rational::output(ostream& out_s)
{
if(denominator != 1)
out_s << numerator << "/" << denominator;
else
out_s << numerator;
}

void Rational::input(FILE *f)
{
if((f=fopen("matrix.txt","r"))==NULL)
{
cout<< "Error opening file.";
return;
}
fscanf(f, "%d/%d' '", &numerator, &denominator);

}

void Rational::operator =(const Rational &a)
{
numerator = a.numerator;
denominator = a.denominator;
return;
}

last the file i'm trying to read from
Code Snippet

4

4

-1 -4 -3 -3
-1 <= 1 1 0 -1
2 >= 1 -1 -1 -1
-1 >= 1 1 0 -1
-2 >= -1 1 -1 1

I certainly appreciate any help on this issue.


Re: Visual C++ Language fscanf won't read past first character

Marius Bancila

What exactly do you mean with can't read past the first char What if the first number is not 4 but 14; does it read 1 or 14

Why don't you use STL streams such as ifstream, instead of C's FILE






Re: Visual C++ Language fscanf won't read past first character

*Cole*

It will read 14. I've noticed that the problem is with the temp variable that I'm using. It never changes. So what I'm wondering if I possibly need a destructor. I'm using C's file because I need to read the file step by step in order to get my data into the array. If you have a way to do this using STL streams let me know.




Re: Visual C++ Language fscanf won't read past first character

*Cole*

I've tried creating another rational variable and it still just reads in the first item. So I no longer believe that it is a destructor issue.




Re: Visual C++ Language fscanf won't read past first character

*Cole*

I believe it is now an issue with passing the file from the main program to the rational class. For some reason when the file is passed it goes back to the beginning of the file.




Re: Visual C++ Language fscanf won't read past first character

*Cole*

I feel like such a moron. I was reopening the file when i entered into my rational class in its input function.




Re: Visual C++ Language fscanf won't read past first character

Marius Bancila

I'm using C's file because I need to read the file step by step in order to get my data into the array.

And who exactly told you can do that only in C

Here are some examples of what you can do.

ifstream file;

file.open("input.txt");

if(!file.is_open())

return ...;

int first;

file >> first;

double val;

for(int i = 0; i < 10; i++)

file >> val;

char ch;

while(!file.eof())

file >> ch;






Re: Visual C++ Language fscanf won't read past first character

*Cole*

I was actually told that it is much easier in C. No worries though, I have it working and doing what I need. Thanks for your help though.



Re: Visual C++ Language fscanf won't read past first character

Marius Bancila

*Cole* wrote:
I was actually told that it is much easier in C. No worries though, I have it working and doing what I need. Thanks for your help though.

I disagree. C++ is much easier to work with, in my opinion. You didn't code it in C, but in C++. But you used C-like streams, instead of OO C++ streams.