fstream

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

fstream

dmoeller
I am coding in c++ and I am using f stream to load my files. How can i have my program load a file with just the .vm extension? for example:

string inFile = (ifstream.open(*.vm));

how can I get something like that to work? and for project 8, how can I get it to load ALL .vm files and produce 1 .asm file?
Reply | Threaded
Open this post in threaded view
|

Re: fstream

cadet1620
Administrator
dmoeller wrote
I am coding in c++ and I am using f stream to load my files. How can i have my program load a file with just the .vm extension? for example:

string inFile = (ifstream.open(*.vm));

how can I get something like that to work? and for project 8, how can I get it to load ALL .vm files and produce 1 .asm file?
ifstream.open() needs a single filename.

You need to check if the filename passed to your VM translator is a directory or a file, and if it is a directory you need to read the directory and process the files that match *.vm one at a time.

Unfortunately, the current C++ standard library has no functions that deal with directories. You will need to fall back to using C library functions to do this.  The functions are different Windows and Mac/linux.

Google for "linux stat" or "windows stat" should get you started.

What OS and compiler are you using?

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: fstream

dmoeller
I am using windows 10 and microsoft visual studio compiler. if fstream requires a single file, what can I use to load a file where the name is unkown but the file extension is known? what can I use to load all files of a given extension?
Reply | Threaded
Open this post in threaded view
|

Re: fstream

cadet1620
Administrator
dmoeller wrote
I am using windows 10 and microsoft visual studio compiler. if fstream requires a single file, what can I use to load a file where the name is unkown but the file extension is known? what can I use to load all files of a given extension?
You need to search the directory that contains the files.  For each file that is found you need to individually process the file.

I don't have any code samples that do this in Microsoft land.  The Windows API functions you want to use to list all files in a directory are  FindFirstFile, FindNextFile, and FindClose.

Microsoft's "simple" (not!) example is here:
    https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx

You can search for only .vm files by changing this code
    // Prepare string for use with FindFile functions.  First, copy the
    // string to a buffer, then append '\*' to the directory name.

    StringCchCopy(szDir, MAX_PATH, argv[1]);
--  StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
++  StringCchCat(szDir, MAX_PATH, TEXT("\\*.vm"));

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: fstream

cadet1620
Administrator
In reply to this post by dmoeller
Coffee break... Here's a truly simple example of directory searching in Windows.
[D:/tmp/c]
% cat vmfiles.c

// vmfiles.c -- Windows directory search example

#include <windows.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    char path[MAX_PATH];
    HANDLE hFind;
    WIN32_FIND_DATA data;

    if (argc < 2) {
        printf("usage: vmfiles <dir>\n");
        return -1;
    }

    printf("VM files in %s:\n", argv[1]);

    strncpy (path, argv[1], MAX_PATH);      // use std::string in C++
    strncat (path, "\\*.vm", MAX_PATH);

    hFind = FindFirstFile(path, &data);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            printf("%s\n", data.cFileName);
        } while (FindNextFile(hFind, &data));
        FindClose(hFind);
    }

    return 0;
}

[D:/tmp/c]
% vmfiles test
VM files in test:
Main.vm
Sys.vm
[D:/tmp/c]
% dir test
 Volume in drive D is SHAREDDATA
 Volume Serial Number is FC26-C168

 Directory of D:\tmp\c\test

10/14/2016  11:23 AM    <DIR>          .
10/14/2016  11:23 AM    <DIR>          ..
10/14/2016  11:30 AM                42 FibonacciElement.cmp
10/14/2016  11:30 AM               516 FibonacciElement.tst
10/14/2016  11:30 AM               484 FibonacciElementVME.tst
10/14/2016  11:30 AM               988 Main.vm
10/14/2016  11:30 AM               606 Sys.vm
10/14/2016  11:42 AM             5,363 FibonacciElement.asm
               6 File(s)          7,999 bytes
               2 Dir(s)  22,740,402,176 bytes free

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: fstream

dmoeller
is MAX_PATH a c++ command or the file path of the directory? if the files im supposed to translate are .tst files can i change the ".vm" to ".tst" and have it still work?
Reply | Threaded
Open this post in threaded view
|

Re: fstream

cadet1620
Administrator
dmoeller wrote
is MAX_PATH a c++ command or the file path of the directory? if the files im supposed to translate are .tst files can i change the ".vm" to ".tst" and have it still work?
MAX_PATH is a predefined constant that comes from <windows.h>  Note that you should not need MAX_PATH if you use std::string in C++ instead of the old C-style char* strings.

You are only going to translate the .vm files.  The *VME.tst files are test scripts to be loaded into the VMEmulator so that you can step through the .vm files to see what is supposed to happen with the VM commands.  The *.tst files (without VME) are test scripts to be loaded into the CPUEmulator to test the .asm files that are generated by your VM translator.

--Mark