Need to adapt a very small code to visual C++ 6.0.

Hi,
I need to convert the cpp code that I wrote in visual studio 2010 to visual C++ 6.0. However, I am getting errors.
I need to do this as I am designing some sequence for MRI scanner and siemens has only visual C++ 6.0 on the scanner.
I need to do very simple operations. I need to change directory and read two values from .txt file and convert it as double.

Also, it has to run on window as well as linux system as scanner uses both.

code in visual studio 2010:
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

#include "stdafx.h"
//#include "test.h" 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


#ifdef WIN32

	#include <direct.h>
	#include <stdlib.h>
	#include <stdio.h>
    #define GetCurrentDir _getcwd
#else
    #include <unistd.h>
    #define GetCurrentDir getcwd
 #endif

int main()
{
	string STRING;
	ifstream infile;

	//Finding out the directory
	char* pwd;
	   // Get the current working directory: 
	   if( (pwd = _getcwd( NULL, 0 )) == NULL )
		  perror( "_getcwd error" );
	   else
	   {
		  printf( "%s \nLength: %d\n", pwd, strlen(pwd) );
		  
	   }

	   //Conversion from char* to string
	   std::string PWD(pwd);

	//printf ("The current working directory is %s", buffer);
	string Filename =  PWD + '/' + "example.txt";

	infile.open (Filename);
	cout<<"Verifying that cout works"<<endl;
        while(!infile.eof()) // To get you all the lines.
        {
			cout<<"works fine here 1"<<endl;
	        getline(infile,STRING); // Saves the line in STRING.
			cout<<"works fine here 2"<<endl;
	        cout<<STRING<<endl; // Prints our STRING.
        }
	infile.close();
	Get_PWD();
	system ("pause");
	free(pwd);
	return 0;
}

Last edited on
Why not tell us the error?
Thanks a lot for responding.

The problem is with stdafx.h. The compiler was complaining for that; so, I downloaded one from internet and included that header file.

Now, it's throwing multiple errors from within stdafx.h

I have put the screen shot on googledoc; please have a look.

https://docs.google.com/open?id=0B0Ah9soYnrlINU15SU55Q29FLUk



Hi,

I was suggested by a friend to replace
#include "stdafx.h"

by
#include WIN32_LEAN_AND_MEAN
#include <winsock2.h>

Now, I am getting a single error.
error C2664: 'open': cannot convert parameter 1 from 'class _STL::basic_strring<char, class _STL::char_traits<char>, class _STL::allocator<char>>' to 'const char *'

No user-defined-conversion operator available that can perform this conversion, or the operator can not be called.
Last edited on
infile.open (Filename);
Should be:
infile.open (Filename.c_str());

Visual Studio 6.0 supports C++98 (1998) to a fair degree (be sure to download Service Pack 6, if you can find it).

Current standard is C++11 (2011), in which the first code snippet would work.
Thanks Catfish2.

You solved my problem.
I still have problem with converting to double.

My text file looks like:

! this is a comment
! this is another comment
1.2
0.9

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
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
//#include "test.h" 
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;


#ifdef WIN32

	#include <direct.h>
	#include <stdlib.h>
	#include <stdio.h>
    #define GetCurrentDir _getcwd
#else
    #include <unistd.h>
    #define GetCurrentDir getcwd
 #endif

int main()
{
	string STRING;
	ifstream infile;

	//Finding out the directory
	char* pwd;
	   // Get the current working directory: 
	   if( (pwd = _getcwd( NULL, 0 )) == NULL )
		  perror( "_getcwd error" );
	   else
	   {
		  printf( "%s \nLength: %d\n", pwd, strlen(pwd) );
		  
	   }

	   //Conversion from char* to string
	   std::string PWD(pwd);

	//printf ("The current working directory is %s", buffer);
	string Filename =  PWD + '/' + "example.txt";

	infile.open (Filename.str());
        while(!infile.eof()) // To get you all the lines.
        {
	        getline(infile,STRING); // Saves the line in STRING.
	        cout<<STRING<<endl; // Prints our STRING.
                if (STRING[1] /= '!'){

				cout<<STRING<<endl; // Prints our STRING.

				//Verify if d is double
				//Numeric input validation
				d = (double)atof(STRING.c_str());
				d1 = d*(double)5.0;
				std::cout<<"Value of d1 is "<<d1<<endl;

                 }
        }
	infile.close();
	Get_PWD();
	system ("pause");
	free(pwd);
	return 0;
}

if (STRING[1] /= '!')

What is this meant to do?
I am skipping the comment using
if (STRING[1] /= '!')
(STRING[1] /= '!')

This is an instruction to divide the character at STRING[1] by the character !, and to store the result as the new STRING[1].

Is that what you meant to do?
Ok,

Thanks a lot Moschops. I appreciate your help and everything works now.
This is an extension of my previous code. Also, reminding you that I have to program this in visual studio 6.0 as MRI scanner is only supporting that.

I am reading value of two entries from txt file and saving it as double. Everything is fine. From main, I can read correctly here

for (int i = 0; i < 2; i++){
std::cout<<"Value read "<<Data1[i]<<endl;
}

but, not here:
double FAIncrement_REFOCUS = Data1[1];
double SliceThick_REFOCUS = Data1[2];

Please help.

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
71
72
73
74
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
//#include <window.h>

//#include "stdafx.h"
//#include "test.h" 
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

#ifdef WIN32
	#include <direct.h>
	#include <stdlib.h>
	#include <stdio.h>
    #define GetCurrentDir _getcwd
#else
    #include <unistd.h>
    #define GetCurrentDir getcwd
 #endif

double*  Read_the_File(string Filename)
{	//Returns a pointer of double


	string STRING;
	ifstream infile;
	double* Data_Pointer = new double[2]; 
	int count = 0;
	infile.open (Filename.c_str());
        while(!infile.eof()) // To get you all the lines.
        {
			
			if (infile.good()){
				getline(infile, STRING); // Saves the line in STRING.

				if (STRING[0] == '!'){
					//std::cout<<"Comment SECTION"<<endl;
				}else{
					
					//std::cout<<"Non-comment section"<<endl;
					Data_Pointer[count] = (double)atof(STRING.c_str());
					count = count +1;
				}


			}else{
				//cout<<"Invalid Input through file"<<endl;
				infile.clear();
				infile.ignore(100000, '\n');
			}

        }
	infile.close();
	return Data_Pointer; 


}

int main()
{
	string Filename1 =  "C:/example1.txt";
	double* Data1 = new double[2]; 
	Data1 = Read_the_File(Filename1);

	double FAIncrement_REFOCUS = Data1[1];
	double SliceThick_REFOCUS = Data1[2]; 

	for (int i = 0; i < 2; i++){
		std::cout<<"Value read "<<Data1[i]<<endl;
	}
	return 0;
}


Last edited on
Data1[2] does not exist. Arrays start at zero.
Last edited on
Thanks,

I am so stupid.
Topic archived. No new replies allowed.