This is going to be a simple question, but it needs a simple answer (i.e I don't know C++ beyond the simple). Please.
I've inherited an old application to manage. Part of it allows for user defined functions. I've been able to write a few very simple functions (read files, return simple string searches) but this is a step above.
I need to convert a single line file, TAB separated, into individual variables. TAB separated because the columns have differing lengths!
Eg. 1234 ABCD WXY 789.
1234 ABCDE RTSU 6789.
Without using anything but simple C++ programming, because there is no way I can update the C-compiler.
I realize that posting the whole code may not be something that you can do, but for now how about the read function so I can what you have to work with and we can work from there.
Right now all I can do is make guesses at what you could do. And they are likely to be beyond what your compiler can handle.
This is the extent of my knowledge.
I wrote this.
As you can see, nothing special.
I need a function that does something like reading the file (only one line), splitting it, and returning the needed array element.
After the code is the includes I have to deal with.
Thanks.
(I'm in Australia, so I will respond tomorrow!)
/*-------------------------------------------------------------------------
*
* Function: FILESTR2 --- TTCN Defined Function ---
*
* Description: This function gets a number of characters from a file.
* Start and stop position used.
*
* Parameters: In - GWOP
*
* Returns: Text from file.
*
*------------------------------------------------------------------------*/
char *FILESTR2(t_StringType *GWOP,t_IntegerType *start,t_IntegerType *stop)
{
FILE *grop;
char *p1str;
char nline[200];
char nline2[200];
char theerr[200];
char *retStr;
int index = 0;
int i;
int ii;
i=0;
int startat = start->_value;
int stopat = stop->_value;
/* Log Function Entry */
AppDiag("userdefttcn FILESTR2(%s)\n", ISNULL(GWOP->_value, "NULL"));
This is what I have to work with:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <malloc.h>
#include <signal.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/unistd.h>
#include <netinet/in.h>
There are a couple things you will need to explain because some of the header files that you are using are not available to me with my newer compiler.
The types "t_StringType" and "t_IntegerType" are unavailable to me, so if you could give me an idea of what these are aliases for it would help.
My C is a little rusty, so it is taking me longer having to look up how some of the code is written and used.
Things I have noticed:
"p1str" is defined as a char pointer. this will hold an address only. The line p1str = GWOP->_value; appears to be trying to put a value in "p1str". If this value is not an address there will be a problem.
I am not sure what "GWOP" is, but my guess would be a struct.
Inside the do/while loop "sleep" should be "Sleep()" and you will need the header file <windows.h>. Also the number in the () is miliseconds where 1000 = 1 second. Your number of 5 is so short that any pause would not be noticed.
The do/while loop not sure what the point is here. It creates a dynamic array, copies a word into it and returns the new string which leaves the function never reaching the while condition, which I do not believe will work in the first place. The condition is based on opening a file. This will work once, but if the do/while loop was written correctly this would be happening after you should be reading the file. I am thinking that the second time you reach the while condition it would fail because the file is already open or you would always be atarting at the beginning of the file.
Because of the do/while loop yo will never reach the lines where you actually read the file. or anything after that because you have already left the function.
The function call to "AppDiag" is a problem for me right now, but I can comment that out and deal with it later.
The header files under "/* ATP includes */" I believe to be files written for this program and I should be able to do with them unless one of them defines "t_StringType" and "t_IntegerType".
Knowing what the input file is and what the function needs to do I will see what I can come up with.
The code I included above has nothing to do with my query.
I just showed it to let you know the extent of my C++ experience.
The code above works.
It's part of a far bigger application that I do not touch. (AppDiag is an inbuilt function.)
I just need a small chunk of code that will split that file.
That there is C, not C++. Unless you are compiling with VS, you should not be expecting C++ functionality. And unless your production environment has been compiling everything with a C++ compiler, you can expect problems with binary compatibility.
I had to start over with a simple program to read the file and was up to the point of checking into the "strtok" function that Duthomhas suggested when I gave up for the day. I will add that to what I have and let you know what I come up with.
I check the text for the "data.txt" I put your information into and found that what I was expecting to be tabs turned out to be spaces. I fixed that.
If you happen to come up with something before me let me know.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <malloc.h>
#include <signal.h>
//#include <pwd.h>
#include <sys/types.h>
//#include <sys/unistd.h>
//#include <netinet/in.h>
#include <Windows.h>
#pragma warning(disable : 4996) // <--- I need this for my compiler. Do not use.
char *FileStr2(FILE* inFile, char* words[10])
{
staticchar line[100];
char* pch;
size_t index = 0;
for (size_t lc = 0; lc < 100; lc++) line[lc] = '\0';
fgets(line, 100, inFile);
pch = strtok(line, "\t");
while (pch != NULL)
{
printf("%s\n", pch);
words[index++] = pch;
pch = strtok(NULL, "\t");
}
puts(line);
//fgets(line, 100, inFile);
return line;
}
int main()
{
char* line;
FILE* inFile;
char* p1str = "Data.txt";
char* words[10]; // <--- Stores the pointers for each work split from a line.
for (size_t lc = 0; lc < 10; lc++) // <--- Sets each element to '\n'. Easier to do in C++.
words[lc] = '\0';
inFile = fopen(p1str, "r");
// This is, for now, set up to read only one line. Multiple calls to the function or a loop would work
// here.
line = FileStr2(inFile, words);
printf("\n\n");
for (size_t lc = 0; lc < 4; lc++) // <--- Used to show what is in the words array. Anything else I tried
// did not work.
printf("%s\n", words[lc]);
fclose(inFile);
return 0;
}