Solved

Checking a specific part of a text file (CSV comma delimited) using tokenizer

Posted on 2011-02-21
5
410 Views
Last Modified: 2012-08-13
This is my code for my double loop...

char str[1024];
char *pch;
FILE *fp;
int first;

fp=fopen("abc.txt", "r");

if(fp != NULL)
while(fgets(str, sizeof(str), fp) != NULL)
{
first=1;
while((pch=strtok(first ? str: NULL, ",")) != NULL)
/*---Here is where I want to insert my line of code---*/
first=0;
}
}

Open in new window


The contents of my text file look similar to this

10/03/2010,NBC,Law&Order
10/02/2010,NBC,Chuck
10/01/2010,NBC,TheEvent
09/29/10,CBS,TheBigBangTheory
09/28/10,CBS,TwoAndAHalfMen
09/27/10,TBS,Conan

At this point, I'm only interested in the second column of the data (the network).
Is there a line that I can insert that will ... If(network=valueA) then perform action A,
Else If (network = valueB) Then perform action B?

Thanks
0
Comment
Question by:--TripWire--
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 34943959
Well, just count the columns that you are processing, i.e.
char str[1024];
char *pch;
FILE *fp;
int first;
int column = 0;

fp=fopen("abc.txt", "r");

if(fp != NULL)
while(fgets(str, sizeof(str), fp) != NULL)
{
first=1;
while((pch=strtok(first ? str: NULL, ",")) != NULL)
/*---Here is where I want to insert my line of code---*/
column++;

  if (column == 2)
  {
    if (!strcmp(pch,"NBC"))
    {
      // process "NBC"
    }

    if (!strcmp(pch,"ABC"))
    {
      // process "ABC"
    }

    if (!strcmp(pch,"CBS"))
    {
      // process "CBS"
    }

    // ,,,
  }

first=0;
}
}

Open in new window

0
 

Author Comment

by:--TripWire--
ID: 34944040
I've decided to use strcpy to place that value into different variable instead.
If I have
char network[3];

Open in new window

How do I strcpy that value in the string variable network?
0
 
LVL 86

Expert Comment

by:jkr
ID: 34944060
The same way - yet your buffer is too small, you need to include the NULL terminator.
0
 

Author Comment

by:--TripWire--
ID: 34944123
Oh ok thanks.  Only, I coded a puts() line after my strcpy (just to check my values) and it's returning the date.

I initialized column to 1 and then column++;
0
 

Author Comment

by:--TripWire--
ID: 34944138
Nevermind...answered my own question!  Thanks again!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question