C: I/O File problems...

Posted on 2007-08-10
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 84

    Expert Comment

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

    Author Comment

    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

    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;
    LVL 11

    Expert Comment

    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

    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
    >> 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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now