Hello everyone! I'm a beginner in C programming, and am writing code that is supposed to read in all the data from a formatted file, parse it into useful values, and then manipulate these values. However, I'm stuck on the the parsing part. I'm relying heavily on pointers, which I'm new with. The problem arises when I attemp to split part of the input file that I've stored into an array. I try to split the barcodes (the values like 0A1A0001) from their 4 to 5 character tolerences (like 0119). While this does work in a way, I get all this random lettering after the good values. In fact, when I run it in unix, it even mess's up unix's prompt, putting it into random character's also! In the code below, I am printing the array where I want all of the barcodes stored as it is built by the while loop. Note that I only have it looping enough to read in the very first line of barcodes and tolerence values for testing purposes. So, here's the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
FILE *ifp;
char Name[16];
char String[1000];
int i=0;
char *ptr=NULL;
ptr=&String[0];
char Date[16];
char *D=NULL;
D=&Date[0];
char Time[16];
char *T=NULL;
T=&Time[0];
char Batch_Num[10];
char *B=NULL;
B=&Batch_Num[0];
char Batch[5];
int Num;
int s;
char *x=NULL;
int Ind=0;
char Barcodes[1000];
char *Ba=NULL;
Ba=&Barcodes[0];
char temp;
int Sind=1;

printf("Please enter a file containing a string:\n");
scanf("%s", Name);
if((ifp=fopen(Name, "r")) == NULL)
{
  printf("No such file!\n");
  return 0;
}
else
{
  ifp=fopen(Name, "r");
}

fgets(Date, 15, ifp);
(*(D+strlen(Date)-1))='\0';

fgets(Time, 15, ifp);
(*(T+strlen(Time)-1))='\0';

fgets(Batch_Num, 8, ifp);
(*(B+strlen(Batch_Num)-1))='\0';

while(i<1)
{
  x=fgets(ptr, 100, ifp);
  if(x==NULL)
  {
    break;
  }
  (*(ptr+(strlen(String)-76*i)-1))='\0';
  i=i+1;
  ptr=ptr+75;
}

fclose(ifp);

for(s=0; s<2; s++)
{
  Num=atoi(B+s+2);
  Batch[s]=*(B+s);
}

ptr=&String[0];

while(Ind<40) 
{
  temp=*ptr;
  *Ba=temp;
  printf("%s\n", Barcodes);
  Ind=Ind+1;
  if(Ind<(8*Sind))
  {
    ptr=ptr+1;
    Ba=Ba+1;
  }
  
  if(Ind==(8*Sind))
  {
    ptr=ptr+8;
    Ba=Ba+1;
    Sind=Sind+1;
  }
}

return 0;
}

And here's what I get when I run it:

0
0A
0A1
0A1A
0A1A0
0A1A00
0A1A000
0A1A0001
0A1A00010
0A1A00010A
0A1A00010A1
0A1A00010A1A
0A1A00010A1A0¸0
0A1A00010A1A00¸0
0A1A00010A1A0000
0A1A00010A1A0002
0A1A00010A1A00020
0A1A00010A1A00020A
0A1A00010A1A00020A1
0A1A00010A1A00020A1Aÿ¿òø
0A1A00010A1A00020A1A0¿òø
0A1A00010A1A00020A1A00òø
0A1A00010A1A00020A1A000ø
0A1A00010A1A00020A1A0003
0A1A00010A1A00020A1A00030
0A1A00010A1A00020A1A00030A
0A1A00010A1A00020A1A00030A1
0A1A00010A1A00020A1A00030A1A
0A1A00010A1A00020A1A00030A1A0
0A1A00010A1A00020A1A00030A1A00
0A1A00010A1A00020A1A00030A1A000
0A1A00010A1A00020A1A00030A1A0004
0A1A00010A1A00020A1A00030A1A00040
0A1A00010A1A00020A1A00030A1A00040A
0A1A00010A1A00020A1A00030A1A00040A1ÿ?CTÿ|Mÿ¿ôŒÿ¿ôÿ•
0A1A00010A1A00020A1A00030A1A00040A1Aÿ?CTÿ|Mÿ¿ôŒÿ¿ôÿ•
0A1A00010A1A00020A1A00030A1A00040A1A0?CTÿ|Mÿ¿ôŒÿ¿ôÿ•
0A1A00010A1A00020A1A00030A1A00040A1A00CTÿ|Mÿ¿ôŒÿ¿ôÿ•
0A1A00010A1A00020A1A00030A1A00040A1A000Tÿ|Mÿ¿ôŒÿ¿ôÿ•
0A1A00010A1A00020A1A00030A1A00040A1A0005ÿ|Mÿ¿ôŒÿ¿ôÿ•

And, lastly, here's the text file being read from:

10-JAN-2010
21:31:55
01-24
0A1A0001 0119 0A1A0002 1017 0A1A0003 0514 0A1A0004 0509 0A1A0005 0884
0A1A0006 0628 0A1A0007 0498 0A1A0008 0539 0A1A0009 0956 0A1A0010 0515
0A1A0011 0678 0A1A0012 0995 0A1A0013 -0033 0A1A0014 0839 0A1A0015 0854
0A1A0016 0822 0A1A0017 1094 0A1A0018 0954 0A1A0019 0498 0A1A0020 0980
0A1A0021 1214 0A1A0022 0498 0A1A0023 -0067 0A1A0024 0342

Thanks in advance!

Narue, you are amazing. Writing newline characters as I built my barcodes array fixed the problem. If the problem re-arises, I'll post back. Thanks again!

This article has been dead for over six months. Start a new discussion instead.