reading and writing files

May 22, 2013 at 11:18am
Hi,

I am completely new to the forum and this is my fist post so I apologize in advance for inappropriate words. I am not a programmer but I have to use c++ because it is amazingly fast. My question is about file loading and saving. In the script below I want to load in a matrix from a ascii txt file and manipulate it then write the new matrix to ascii (or binary but first the ascii should work). Of course I did not want to reinvent the wheel so the code below is from the Internet and was put together for this purpose. If I change the inFile to matrixin.txt and the outFile to matrixout.txt the code works charmingly and the result is also good. So far so good. But when I change back to the command-line read in version the script still can be compiled but I get an error when running the executable. The error is: Segmentation fault! Can anybody see where I did the mistake?

Thank you for your help!
Zolo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "string"
using namespace std;

int main(int argc, char** argv){
    string inFile  = "";
    string outFile = "";
    if( argc == 3 ) {
      inFile = argv[1];
      outFile = argv[2];
    }
    else {
      cout << "Usage: ./cppfile InputFile OutputFile\n";
      return 1;
    }

    int i,j,m,n;

  ifstream in ("inFile");
  in.open("inFile");
     in >> m >> n;
    int A[m][n];
    int B[m][n];
//ifstream file ("matrixin.txt");
//file >> m >> n;

if (m != n)
  {
  cout << "Matrix not squared!" << endl;
  return 1;
  }

for (int i = 0; i < m; i++)
{

for (int j = 0; j < n; j++)
{

  in >> A[i][j];
}

}

in.close();
cout << inFile << endl;
for (int i=0; i < m; i++)
{
for (int j=0; j < n; j++)
{
B[i][j] = A[j][i];
}
}

 ofstream out ("outFile");
 out.open("outFile");
  out << m << " " << n << "\n";
for (int i = 0; i < m; i++)
  {
  for (int j = 0; j < n; j++)
    {
    out << B[i][j] << " ";
    }
  out << "\n";
  }
out.close();
}
May 22, 2013 at 11:27am
Do you have a file named "inFile" in your current working directory? You're never using your variable inFile, you use the string constant "inFile" instead. Notice the quotation marks?

You should always check to insure your file properly open properly before trying to read from that file.

Jim
Last edited on May 22, 2013 at 11:27am
May 22, 2013 at 11:43am
In addition to what jlb said, the following code is not legal C++:
1
2
3
     in >> m >> n;
    int A[m][n];
    int B[m][n];


Expressions used to indicate array dimensions in C++ must be compile time constants.
May 22, 2013 at 2:36pm
Hi,

thanks for the answers. to Jim: I checked the file and it is in the directory so that should be no problem. If I print the string constant to the terminal it gives me the right file name but then if I put it into the brackets as string it somehow doesnt do the job!

to Cire: I thought one can have variable definitions "on the fly" in C++. I guess then I have to declare the variables in the beginning and choose a variable size vector. Or What is the best solution for definition of a matrix(array) which could change size?

 
A= new int[n]


Thanks,
Zolo
May 22, 2013 at 3:04pm
thanks for the answers. to Jim: I checked the file and it is in the directory so that should be no problem. If I print the string constant to the terminal it gives me the right file name but then if I put it into the brackets as string it somehow doesnt do the job!


Read my last post again, carefully. You have a file named inFile in your current working directory?

What is the best solution for definition of a matrix(array) which could change size?


std::vector


Jim

May 22, 2013 at 6:57pm
Hi,

I read your post again and it turns out i didnt reply correctly. I am sorry! You are correct there is no such a file in the directory as inFile. InFile is the second argv[1] read from the commandline:
./trans matrixin.txt matrixout.txt.
So there is a file called matrixin.txt in the directory! So in then matrixin.txt=inFile!

Thanks,
Zolo
May 23, 2013 at 12:25pm
But you aren't using inFile in your open command you're using the const string "inFile", note the quotation marks.

1
2
  ifstream in ("inFile");
  in.open("inFile");


Also why are you trying to open the same file twice? You don't need the in.open() if you use the file name with the constructor.

Jim
May 23, 2013 at 2:46pm
do it like so:

ifstream in (inFile.c_str());
May 27, 2013 at 11:55am
Thanks, it this worked out very well. Now I just have to understand why! :)
 
ifstream in (inFile.c_str());
May 27, 2013 at 12:09pm
Now I just have to understand why!
fstream (C++98) does not take a std::string as a paramter (in the next standard C++11 it will) hence you currently need c_str() to provide the c-string equivalent

see:
http://www.cplusplus.com/reference/fstream/fstream/fstream/
Topic archived. No new replies allowed.