need some help

I am not a c++ programmer, normally i write in vb. But I have a need to imbed some c++ code in a SISS package.

I does a simple process, It finds an @ in each file record and removes the @ and any spaces before the @, writes the record to a temp file with a CR+LF.

My VB code is:

dim strline, posi, txt
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(WScript.Arguments(0), ForReading)
Set objFile = objFSO.CreateTextFile("temp.out",True)
infile = WScript.Arguments(0)

Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine
x=instr(strLine,"@")
txt = (Left(strline,x)) + vbcrlf
objFile.Write txt
loop

objTextFile.Close
objFile.Close

objfso.deletefile(infile)
objfso.movefile "temp.out", infile

I hope someone can show me how to accomplish this in c++ code.




Thanks
closed account (o3hC5Di1)
Hi there,

I don't know VB, but I'll have a go at literally translating your code into C++:

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
#include <string>
#include <fstream>

int main(int argc, char* argv[])
{
    //dim strline, posi, txt
    //Const ForReading = 1

    std::string strline, txt;

    //Set objFSO = CreateObject("Scripting.FileSystemObject")
    //Set objTextFile = objFSO.OpenTextFile(WScript.Arguments(0), ForReading)
    //Set objFile = objFSO.CreateTextFile("temp.out",True)
    //infile = WScript.Arguments(0)

    std::string infile = argv[0];
    std::ifstream objTextFile(infile);
    std::ofstream objFile("temp.out");

    //Do Until objTextFile.AtEndOfStream
    //strLine = objTextFile.ReadLine
    //x=instr(strLine,"@")
    //txt = (Left(strline,x)) + vbcrlf
    //objFile.Write txt
    //loop

    std::string tmp;

    while (objText.good())
    {
        std::getline(objTextFile, tmp);
        size_t at_pos = tmp.find_first_of('@');

        if (at_pos != std::string::npos)
        {
            size_t space_pos = tmp.find_last_not_of(' ', at_pos)-1;
            tmp.erase(space_pos+1, at_pos-space_pos);
        }

        objFile << tmp << "\r\n";
    }    

    //objTextFile.Close
    //objFile.Close

    objTextFile.close();
    objFile.close();

    return 0;
}



I didn't test it, but it should give you a good basis to build on.
For a little more examples and information on the code used:

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/doc/tutorial/files/

Hope that helps, please do let us know if you have any further questions.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.