C: I/O File problems...

Posted on 2007-08-10
Medium Priority
Last Modified: 2010-04-15
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]);
Question by:cap213
LVL 85

Expert Comment

ID: 19673877
for (j = 0; j < 50; j++){
  fscanf(va, "%80s", tite);
  if( strstr(tite, title) )
    sscanf(tite, "%[0-9]",stocknum[j]);

Author Comment

ID: 19674288
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;
LVL 13

Accepted Solution

josgood earned 1000 total points
ID: 19674848
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;

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 11

Expert Comment

ID: 19675534
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);
LVL 22

Expert Comment

ID: 19675732
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 );

LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 19679898
>> 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 [ ].

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
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 this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

839 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