Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
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…

617 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