Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-21
5
Medium Priority
?
442 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

971 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