• C

C: I/O File problems...

So I'm writing a program in C  and this one part is giving me trouble.  I want this function to find the first half of the line, which is set up 348247:title, with only the title. How would I accomplish this?(the title which I am comparing, is passed from another function)

int search(char title[81], int *stocknum[])
int j;
char tite[81];
FILE *va;
va = fopen("/Users/cap412/Desktop/videos.dat", "r");
for (j = 0; j < 50; j++){
fscanf(va, "%s", tite[j]);
if(strncmp(tite, title, 81) == 0)
fscanf(va, "%[0-9]", &stocknum[j]);
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for (j = 0; j < 50; j++){
  fscanf(va, "%80s", tite);
  if( strstr(tite, title) )
    sscanf(tite, "%[0-9]",stocknum[j]);
cap213Author Commented:
Hrm...this only searches and inputs the first value in the database...maybe I'm doing something wrong when the user scans the title?

int getTitle(char title[81])
      printf("Please enter title to search or quit:  ");
      scanf("%s", title);
      return title;
int search(char title[81], int *stocknum[])
   int j, offset;
   char tite[81];
   FILE *va = fopen("your file", "rt");
   int k = errno;
   for (j = 0; j < 50; j++){
      fscanf(va, "%s", tite);
      offset = strstr(tite, title);
      if (offset != 0)
         sscanf(tite, "%d", &stocknum[j]);
   return 1;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SolarWinds® Network Configuration Manager (NCM)

SolarWinds® Network Configuration Manager brings structure and peace of mind to configuration management. Bulk config deployment, automatic backups, change detection, vulnerability assessments, and config change templates reduce the time needed for repetitive tasks.

Change the loop body to something like this

/* Get the line in */
char buffer[80];
fgets (buffer, sizeof (buffer), va);

/* Get the stock number - stops converting as soon as it hits a non numeric */
stocknum[j] = atoi (buffer);

/* Get title one char after the colon */
strcpy (title[i], strchr (buffer, ':') + 1);
I'd use this approach, a teensy bit slower but simpler:

int stocknumarray[10000];   char stocktitlearray[10000][80];  FILE * va;

va = fopen( "stockfile.txt", "r" ); tot = 0;

if( va != NULL ) {

while( not feof( va ) ) {
fscanf( va, "%d:%s", &stocknumarray[ tot ], stocktitlearray[ tot ]);
if(  strstr( title, WantedTitle ) != NULL )     tot++;

fclose( va );

Jinesh KamdarCommented:
>> maybe I'm doing something wrong when the user scans the title?
I guess so, looking at ur getTitle function since it is defined to return an int, but is actually returning a char [ ].
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.