Thread: issue while printing

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    130

    issue while printing

    I've already posted this code in my former posts, but the issue here is different, I want to print the name that's given in the input.txt, also supposing I can't change the array's name size as its size stable as 4, but in the sscanf that's would make a serious problem, can anybody suggest me away to
    solve the problem? thanks in advance, and once again,
    supposing that I cant change the size of the array that's
    inculded in the struct.

    Code:
    #include <string.h>
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    typedef struct
    {
        char name[4];// the name of the document-in my case documt
    }memIdentity;
    memIdentity *Array[100]={0};
    void Wikipedia_poll(char name[4], memIdentity *Array[100])
    {
        int i=0;
        for(i=0; i<100; i++)
        {
            if (Array[i] == NULL)
            {
                Array[i]= (memIdentity*) malloc(sizeof(memIdentity));
                strcpy(Array[i]->name,name);
                printf("%s", Array[i]->name);
                Array[i]->size = size;
                break;
            }
        }
        return;
    }
    void print_result()
    {
        int i=0;
        for (i=0; i<100; i++)
        {
            if (Array[i]!=NULL)
                printf("%s", Array[i]->name);
                            //here..the problem here is actually not printing the given name//
            if (Array[i]==NULL)
                printf("H");    
        }
    }int main()
    {    
        FILE *pToFile = fopen("C:/input.txt","r");
        char line[30];
        char name[4];
        int j=0;
        while( fgets( line, 30, pToFile )) 
        {
            if (strstr(line, "Cboard OP'S") == line)
                    if (sscanf(line,"%*[^:]: %*s %4s %*s",name) =1)
             Wikipedia_poll(name);
            if (strstr(line, "print result") == line)
                print_result();
        }
        fclose(pToFile);
        return 0;
    }
    

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have bigger problems than trying to print a name.

    Code:
    /*
    main.c||In function 'Wikipedia_poll':|
    main.c|19|error: 'memIdentity' has no member named 'size'|
    main.c|19|error: 'size' undeclared (first use in this function)|
    main.c|19|error: (Each undeclared identifier is reported only once|
    main.c|19|error: for each function it appears in.)|
    main.c||In function 'main':|
    main.c|45|error: lvalue required as left operand of assignment|
    main.c|46|error: too few arguments to function 'Wikipedia_poll'|
    main.c|41|warning: unused variable 'j'|
    ||=== Build finished: 6 errors, 1 warnings ===|
    */
    You also don't specify what is contained inside of "input.txt".

    ... I want to print the name that's given in the input.txt, also supposing I can't change the array's name size as its size stable as 4, but in the sscanf that's would make a serious problem ...
    You are not describing your problem clearly. You say the array size is fixed, but how would this make "sscanf()" have a serious problem? Is the input longer than the array can hold? Are there unexpected characters in the input file? Another reason entirely? (See why it's important to give us as much information as you can?)

    While we wait for a more descriptive explanation of the problem you're facing, I'll offer some general advice. Put this code aside for a moment, and create a new simple program that only handles this one specific task (successfully reading input from the text file, I suppose). If you can get this to work, then go back to your original program and incorporate that code. If you can't get the simple version to work, then post just that, and we can offer help more focused to the specific problem you're describing.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    documt cannot be stored in the array

    char name[4]

    only

    doc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by vart View Post
    documt cannot be stored in the array

    char name[4]

    only

    doc
    So how can I let it prints docu? with four chars without changing the size of array's name

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You can change the size of an array, if you don't declare it as an array. Instead of declaring the struct as:

    Code:
    typedef struct
    {
        char name[4];// the name of the document-in my case documt
    }memIdentity;
    Instead declare it as:

    Code:
    typedef struct
    {
        char *name;// the name of the document-in my case documt
        int size;
    }memIdentity;
    Then you could have a few helper functions to go with it:

    Code:
    memIdentity *memIdentity_create(int size); // should return NULL on failure
    int memIdentity_resize(int size); // should return a value to indicate success or failure
    void memIdentity_destroy(memIdentity *mem);
    The implementation of these functions is up to you. You should google dynamic memory management in C. You'll learn about functions malloc, realloc, and free.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Romyo2 View Post
    So how can I let it prints docu? with four chars without changing the size of array's name
    If you want to print this as a string, then the array can only hold "doc" as mentioned by Vart. The fourth element must be reserved for the null character '\0'.

    If you want the array to hold "docu", then it cannot be printed as string, and you need to print each element of the array separately.

    I'd also strongly suggest making a separate function that handles the string parsing. Trying to cram complicated string parsing into a single line of code is not a good idea in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing's issue.
    By RyanC in forum C Programming
    Replies: 36
    Last Post: 06-11-2015, 08:24 AM
  2. need with debaging - printing issue
    By ilans11il in forum C Programming
    Replies: 5
    Last Post: 02-26-2013, 08:32 AM
  3. printing issue
    By ilans11il in forum C Programming
    Replies: 3
    Last Post: 02-21-2013, 12:53 AM
  4. Strange issue with printing array value
    By CodeKate in forum C Programming
    Replies: 6
    Last Post: 11-24-2010, 04:52 PM
  5. Issue with printing a varying array
    By monki000 in forum C++ Programming
    Replies: 10
    Last Post: 04-19-2010, 04:06 PM