Solved

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

Posted on 2011-02-21
5
394 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now