Single line file, split into variables by TAB.

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.

Please!

Thanks.
Last edited on
Hello rush2112,

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.

Andy
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"));

p1str = GWOP->_value;
do{
i++;
if(i>1)sleep(5);
if(i>10)
{
retStr = malloc(6);
strcpy(retStr, "Faulty");
return(retStr);
}
}while((grop = fopen(p1str,"rt"))==NULL);

fgets(nline,199,grop);
fclose(grop);
retStr = malloc(20);
nline[100]=0;
/* remove(p1str); */

for(ii=startat; ii<stopat; ii++)
{
nline2[index]=nline[ii];
index++;
}

nline2[index]=0;
strcpy(retStr, nline2);
return(retStr);
free(retStr);
}

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>

/* ATP includes */
#include <machdeps.h>
#include <AppDiag.h>
#include <RunLogMsg.h>
#include <AdaptorErr.h>
#include <atppprims.h>
#include <ATPP.h>
#include <ErrorLog.h>
#include <FaultLog.h>
#include <CiTRStdlib.h>
#include <Buffer.h>
#include <dirread.h>
#include <PacketTracing.h>
#include <PacketDebug.h>
#include <ATPConfig.h>
#include "generic.h"
#include "Args.h"
#include "FileCase.h"

/* DANET Includes */
#include "cb_typedef.h"
Hello rush2112,

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.

Hope that helps,

Andy
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.

In any case, to answer your question, to split a string on tabs use a mutable string an strtok(). Examples exist with the documentation:
http://www.cplusplus.com/reference/cstring/strtok/

Good luck!

[edit]
Oh, to convert things from string to int/long, use atoi()/atol() or strtol().

@rush2112: most of the includes and other stuff OP posted are application-specific.
Last edited on
G'Day rush2112,

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.

Andy
G'Day rush2112,

I could not use what you had given me do I came up with something to test out what I was thinking of.

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
#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])
{
	static char 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;
}


Hope that helps,

Andy
Topic archived. No new replies allowed.