Solved

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

Posted on 2011-02-21
5
415 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--
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

763 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