Thread: Problem with .txt file

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Problem with .txt file

    I have this problem with binary files. I cant access the file properly. Here is a portion of my program:-

    main(){
    char name[1000];
    int x,num;
    FILE *fptr;
    fptr=fopen("name.txt","w");
    printf("Enter number of names: ");
    scanf("%d", &num);
    for(x=0;x<num;x++){
    printf("Enter name: ");
    scanf("%s", name);
    fwrite(&name, sizeof(name),1,fptr);
    }
    fclose(fptr);
    fptr=fopen("name.txt", "r");
    while(fgets(name,1,fptr)!=NULL){
    fscanf(fptr,"%s",&name);
    ...
    ...
    }
    }

    the problem is that i cant read more than 1 name in the list
    If I save more than 1 name in the .txt file, then when i try to read it, there will be some error.Please HELP. I'm a beginner.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    a) I will move this to the C Borad as soon as I posted this reply. It's not C#.

    b) When writing/reading files, always use coresponding pairs of read/write functions. When you write your data with fwrite, you need fread to read it. If you want to read it with fgets, the best way would be to use fputs to write it. fprintf/fscanf would be another pair. It's not the only solution, but the simplest and least error prone.

    You don't need any binary stuff here. It's a textfile. I'd suggest using fscanf/fprintf. In your loop, you read two times each loop. Once in the while condition with fgets, then a second time with fscanf. You only need to read once inside your loop. Use EOF to check if you are at the end of the file.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Do like this:

    while (fscanf(fptr, "%s", &name) != EOF)
    {
    fread(&name, sizeof(name), 1, fptr);
    printf("Name: %s", name);
    }
    Last edited by GaPe; 12-24-2001 at 04:02 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    5
    There's still a problem with the program.
    The error only occurs when I enter more than 1 name.
    An error screen will popup saying that this program has performed an illegal operation.
    Why is this happening?

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Ups, a mistake, fixed code:

    int i;

    for (i=0; i<=NoOfNames; i++)
    {
    fread(&name[i], sizeof(name), 1, fptr);
    printf("Name: %s", name);
    }

    it's the same with fwrite. If you use it in for loop you have to write it like this:


    for(x=0;x<num;x++)
    {
    printf("Enter name: ");
    scanf("%s", name);
    fwrite(&name[x], sizeof(name),1,fptr);
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    5
    okok...i got it thanx alot u guys but there's still another problem regarding the fscanf or fgets call
    I've modified my program to

    main(){
    char name[100];
    int x,name;
    FILE *fptr;
    fptr=fopen("name.txt","w");
    printf("Enter number of names: ");
    scanf("%d", &num);
    for(x=0;x<num;x++){
    printf("Enter name: ");
    scanf("%s", name);
    fwrite(&name, sizeof(name),1,fptr);
    }
    fclose(fptr);
    fptr=fopen("name.txt", "r");
    while (fscanf(fptr, "%s", &name) != EOF) {
    fread(&name, sizeof(name), 1, fptr);
    printf("Name: %s", name);
    }
    }

    the problem is that the fread doesnt jump to the nextline in the txt file and it doesnt read the next name in the list

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Look at my code.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at my code.
    Code:
    Ups, a mistake, fixed code: 
    
    int i; 
    
    for (i=0; i<=NoOfNames; i++)
    Ok. There's a problem with it. That should be < and not <=

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM