Thread: c program

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    5

    c program

    Hi, im pretty new to C

    im trying to make a program that will read a input.dat file
    John Smith
    Ben Some
    Jeff More

    and output it to output.dat file but
    Smith John
    Some Ben
    Jeff More

    using sequential file

    Thank you

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    5
    well i can do that but when i use fgetc
    but im pretty sure im suppose to use fgets for that

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    5
    ok i've heard of scanf but whats sscanf
    and one more thing please
    how do i exchange the First name and last name
    John Smith
    to
    Smith John
    once i assigned the values to variables how do i output it

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    5
    #include <stdio.h>
    #include <stdlib.h>

    FILE *fp, *out;
    main()
    {
    char FName;
    char LName;
    char ch;


    if((fp = fopen("client.dat","r")) == NULL)
    {
    printf("\nNo file opened.");
    exit(1);
    }

    if((out = fopen("output.dat", "w")) == NULL)
    {
    printf("\nNo output file opened.");
    exit(1);
    }

    while fgets(ch, 0, fp) != EOF) (
    scanf("%s", &FName);
    scanf("%s", &LName);
    fputs(&ch, out);

    fclose(fp);
    fclose(out);

    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code.

    Here is your original code, with some modifications.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      FILE  *fp, *out;
      char  FName[100];
      char  LName[100];
      char  line[100];
    
      if ((fp = fopen("client.dat", "r")) == NULL)
      {
        printf("\nNo file opened.\n");
        return (EXIT_FAILURE);
      }
    
      if ((out = fopen("output.dat", "w")) == NULL)
      {
        printf("\nNo output file opened.");
        return (EXIT_FAILURE);
      }
      
      while (fgets(line, sizeof(line), fp)  != NULL)
      {
        sscanf(line, "%s%s", FName, LName);
        fprintf(out, "%s %s\n", LName, FName);
      }
      
      fclose(fp);
      fclose(out);
      
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    5
    thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM