text files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cv87cv
    New Member
    • Dec 2006
    • 7

    text files

    I am trying to answer a question with regards to entering a set of numbers and text, and then I have got to search for a number and the text with it.

    This is the code that I have written for inputting the numbers:

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    using namespace std;

    int main()
    {
    char name[20];
    int mark;
    int x;
    FILE *f;
    f = fopen("black.tx t","w");
    for(x=1; x<=5; x++)
    {
    printf("Enter a name \n");
    gets(name);
    printf("Enter a mark \n");
    scanf("%d",&mar k);
    fprintf(f,"%s %d\n",name, mark);
    }

    fclose(f);
    return 0;
    }

    it will not input the number and mark correctly together

    the other part of the code to display a certain number and text is :


    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    using namespace std;

    int main()
    {
    char name[20];
    int number;
    int x;
    FILE *f;
    f = fopen("black.tx t","w");
    while ( fscanf(f,ā€%sā€,n ame )
    {
    if (number=10)
    {
    printf(ā€œ%s \nā€,name);
    }
    }
    fclose(f);
    return 0;
    }

    this part is not working properly either and I am unable to spot what is wrong with it. i think because i've been looking at it for a while i just cant see the problems

    so if someone can help me that would be great
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by cv87cv
    I am trying to answer a question with regards to entering a set of numbers and text, and then I have got to search for a number and the text with it.

    so if someone can help me that would be great
    how do you want to format the name and mark because that determines how you read the information, e.g.
    (1) two lines
    Sam Jones
    45
    (2) one line
    Sam Jones 45

    will you always have a fist name and last name, can you have a middle name, etc?

    Comment

    • cv87cv
      New Member
      • Dec 2006
      • 7

      #3
      Originally posted by horace1
      how do you want to format the name and mark because that determines how you read the information, e.g.
      (1) two lines
      Sam Jones
      45
      (2) one line
      Sam Jones 45

      will you always have a fist name and last name, can you have a middle name, etc?
      yea there will always be a first and last name, no middle name. the name will be on a different line to the number which is given to each person.

      Comment

      • svsandeep
        New Member
        • Nov 2006
        • 15

        #4
        http://www.thescripts. com/forum/thread576670.ht ml

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by cv87cv
          yea there will always be a first and last name, no middle name. the name will be on a different line to the number which is given to each person.
          you could have seperate variables for first and last name, e.g.
          Code:
          #include <stdio.h>
          
          int main()
          {
          char firstname[20],lastname[20];
          int mark;
          int x;
          FILE *f;
          f = fopen("black.txt","w");
          for(x=1; x<=5; x++)
          {
          printf("Enter a name \n");
          scanf("%s%s",firstname, lastname);
          printf("Enter a mark \n");
          scanf("%d",&mark);
          fprintf(f,"%s %s mark %d\n",firstname, lastname, mark);
          }
          fclose(f);
          return 0;
          }
          are you planning to use C or C++ as you had included header files from both?

          Comment

          Working...