Remove cr From String.

Several years ago I inherited a C+ (I don't think it's C++) application to maintain. Every now and then I'm asked to include a new function in it.
I know very basic C+ programming, and much of the time I re-using other parts of the program, tweeking it to do something new.

Here's my problem.
This function reads a single line, comma-separated, text file, and splits it into an array. The function is then called with a pointer to the relevant array cell, returning that value.
The thing is, the last value (pointer = "id" below) has a CR/LF included, which is problematic to the calling application.

How does one remove that pesky CR/LF?
Again, I'm not an experienced C++ programmer. The code below was copied from another part of the program, with slight changes from me.
Any help would be appreciated.
Thanks very much.

char *GETDATACSV(t_StringType *GWOP)
{
FILE *grop;
char *p1str;
char nline[200];
char theerr[200];
static char z_fn[128];
static boolean firstpass = TRUE;
static char tstr[200];
char *tsts;
static char retStr[14][128] = {"","","","","","","","","","","","",""};
int p;

sprintf(z_fn, "/abc/files/%s%s.info", args[ARGS_NAME].value, args[ARGS_JOBID].value);

p1str = GWOP->_value;
if(firstpass)
{
if((grop = fopen(z_fn,"r"))==NULL)
{
return("bad stuff");
}
firstpass = FALSE;
fgets(tstr,124,grop);
tsts = tstr;
for(p=0;p<13;p++)
{
strlcpy(retStr[p],tsts,strcspn(tsts, ",")+1);
tsts = strstr(tsts,",") + 1;
}
fclose(grop);
}
if(strcmp(p1str,"seq")==0)return(retStr[0]);
if(strcmp(p1str,"time")==0)return(retStr[1]);
if(strcmp(p1str,"date")==0)return(retStr[2]);
if(strcmp(p1str,"pin")==0)return(retStr[3]);
if(strcmp(p1str,"name")==0)return(retStr[4]);
if(strcmp(p1str,"state")==0)return(retStr[5]);
if(strcmp(p1str,"status")==0)return(retStr[6]);
if(strcmp(p1str,"accnum")==0)return(retStr[7]);
if(strcmp(p1str,"linenum")==0)return(retStr[8]);
if(strcmp(p1str,"fund")==0)return(retStr[9]);
if(strcmp(p1str,"esap")==0)return(retStr[10]);
if(strcmp(p1str,"file")==0)return(retStr[11]);
if(strcmp(p1str,"id")==0)return(retStr[12]);
return("faulty");
}
Try this. (The language is just C, by the way. There is no C+ language.)

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
char *GETDATACSV(t_StringType *GWOP)
{
    FILE *grop;
    char *p1str;
    char nline[200];
    char theerr[200];
    static char z_fn[128];
    static boolean firstpass = TRUE;
    static char tstr[200];
    char *tsts;
    static char retStr[14][128] = {{0}};
    int p;

    sprintf(z_fn, "/abc/files/%s%s.info",
            args[ARGS_NAME].value, args[ARGS_JOBID].value);

    p1str = GWOP->_value;
    if (firstpass)
    {
        if ((grop = fopen(z_fn, "r")) == NULL)
        {
            return("bad stuff");
        }
        firstpass = FALSE;
        fgets(tstr, 124, grop);
        tsts = tstr;
        for (int p = 0; p < 13; p++)
        {
            size_t x = strcspn(tsts, ",\r\n");
            strncpy(retStr[p], tsts, x);
            retStr[p][x] = '\0';
            tsts += x;
            tsts += strspn(tsts, ",\r\n");
        }
        fclose(grop);
    }

    static const char *fields[] = {
        "seq", "time", "data", "pin", "name", "state", "status",
        "accnum", "linenum", "fund", "esap", "file", "id", NULL};
    for (int i = 0; p[i]; i++)
        if (strcmp(p1str, p[i]) == 0)
            return retStr[i];
    return("faulty");
}

Last edited on
The '\n' comes probably from fgets(tstr,124,grop);
'\n' is part of the input and fgets adds the '\n' when less than required chars are read.
You can manually delete it or use your own fgets function like mine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
  Like the standard fgets but removes a trailing '\n'
*/
char* my_fgets(char *str, int num, FILE * stream)
{
  assert(str != NULL); /* #include <assert.h> */
  assert(stream != NULL);
  assert(num > 1);

  char* retval = fgets(str, num, stream);
  char *pos = strrchr(str, '\n');
  if (pos)
    *pos = '\0';

  return retval;
}
Thanks.
I'll give it a go.
Topic archived. No new replies allowed.