• 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]);
fclose(va);
}
}
cap213Asked:
Who is Participating?
 
josgoodCommented:
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]);
         break;
      }
   }
   fclose(va);
   return 1;
}
0
 
ozoCommented:
for (j = 0; j < 50; j++){
  fscanf(va, "%80s", tite);
  if( strstr(tite, title) )
    sscanf(tite, "%[0-9]",stocknum[j]);
}
  fclose(va);
0
 
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;
}
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
cupCommented:
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);
0
 
grg99Commented:
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 );

0
 
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 [ ].
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.