Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

new too C , file opening writeing problem

  • 04-12-2006 7:52pm
    #1
    Registered Users, Registered Users 2 Posts: 695 ✭✭✭


    Heres my code, basically its meant to open a file, scan through it for spaces, and put a new line everytime it finds a space. It runs, and actually prints the message "Found Space, adding new line.." so I assume everything is working ok except for either, the fprintf statement, or the "r+" operator, ive tried loads of combinations but its not working, nothing changes in the file.

    Also, does anyone know a good website for C library explanations?
    #include <stdio.h>
    
    main()
    {
    FILE *fin;
    char fileChar;
    
    
    if ((fin = fopen("primesTEST.txt", "r+")) == NULL)
      {
        printf("Error: can't open file. \n");
      }
    else
      {
        printf("File opened. Formating... \n");
    	
        while ((fileChar = fgetc(fin)) != EOF)
          {
             if (fileChar == ' ')
               {
                printf("Found Space, adding new line.. \n");
                fprintf(fin, "%c", 13);
               }
           }
    	
      fclose(fin);
      printf("Done.");
      }	
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Umm you only put the + there if you adding more onto it so id say it just "r". Havn't looked at the rest but if that doesn't work let me know


  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    Are you really 'sure' that nothing changes?

    You're outputting ASCII 13 which is a carriage return. Assuming that you're on a windows machine a newline in an ASCII text file is formed by a carriage return followed by a line feed (ASCII 10). The file has actually changed but it's missing half the requirement for the newline to appear in your text editor.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Whats wrong with fprintf(fin, "\r\n") these days?


  • Registered Users, Registered Users 2 Posts: 6,570 ✭✭✭daymobrew


    You are writing to the input stream, which is opened for reading, not writing.
    I believe that you should fopen two streams, one for reading the input file and the other for writing a modified file. When you are done you can rename the modified file to overwrite the original one.

    It's a little shorter in perl :p
    perl -p -i.bak -e 's/\s/\n/g' primesTEST.txt
    


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Actually yes you are right. Try:

    fin = fopen("primesTEST.txt", "r+w")


  • Advertisement
  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    daymobrew wrote:
    You are writing to the input stream, which is opened for reading, not writing.
    Opening a file in C with fopen "r+" opens the file at the start for read and write access. Apart from not printing the right characters for a Windows newline the code should do exactly what the OP wants.


  • Registered Users, Registered Users 2 Posts: 6,570 ✭✭✭daymobrew


    Opening a file in C with fopen "r+" opens the file at the start for read and write access.
    Ok, my mistake. I should have read the fopen man page. I was surprised that no one else had reported this (non-)'mistake'.

    I wonder if the following (about using fseek is contributing to the problem), though it didn't make it work on my Solaris system.
    Reads and writes may be intermixed on read/write streams in any order. Note that ANSI C requires that a file positioning function intervene between output and input, unless an input operation encounters end-of-file. (If this condition is not met, then a read is allowed to return the result of writes other than the most recent.) Therefore it is good practice (and indeed sometimes necessary under Linux) to put an fseek or fgetpos operation between write and read operations on such a stream. This operation may be an apparent no-op (as in fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.


  • Registered Users, Registered Users 2 Posts: 695 ✭✭✭DaSilva


    Thanks for the help, basically I figured out there is loads of problems in the code. Like I dont think the program actually goes through the text file at all, im not moving the pointer at all, so I cant imagine its moving from the start of the file.

    Also, sorry for the confusion with the '13', thats a bit of a habit carried on from learning Assembly, its a habit I have to sort out, but its far better than the previous version which was allways using vbcrlf :f


  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    DaSilva wrote:
    Thanks for the help, basically I figured out there is loads of problems in the code. Like I dont think the program actually goes through the text file at all, im not moving the pointer at all, so I cant imagine its moving from the start of the file.
    It does go through the file. The fgetc function will move the file pointer's position forward by one byte every time it is called so it will iterate over the file until 'EOF'. Otherwise your code would be stuck in an infinite loop.


  • Registered Users, Registered Users 2 Posts: 695 ✭✭✭DaSilva


    It does go through the file. The fgetc function will move the file pointer's position forward by one byte every time it is called so it will iterate over the file until 'EOF'. Otherwise your code would be stuck in an infinite loop.

    hmmm funky, in that case it might just be that I need both carriage return and newline.

    Anyone got a good site for explanation on the C library functions?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 695 ✭✭✭DaSilva


    daymobrew wrote:
    Ok, my mistake. I should have read the fopen man page. I was surprised that no one else had reported this (non-)'mistake'.

    I wonder if the following (about using fseek is contributing to the problem), though it didn't make it work on my Solaris system.

    your solution worked, seems you do need a pointer function inbetween reads and writes. Cheers for that.

    Got it printing stuff now anyway, problem is its an infinte loop, as it seens to be finding the same space over and over, probably just a (0, 1, -1) type problem.

    Heres the code now, basically its the same except it includes a "non-op" ( i believe thats what the article called it, fseek function.
    #include <stdio.h>
    
    main()
    {
    FILE *fin;
    char fileChar;
    
    
    if ((fin = fopen("TEST.txt", "r+")) == NULL)
      {
        printf("Error: can't open file. \n");
      }
    else
      {
        printf("File opened. Formating... \n");
    	
        while ((fileChar = fgetc(fin)) != EOF)
          {
             if (fileChar == ' ')
               {
                printf("Found Space, adding new line.. \n");
                fseek(fin, 0L, SEEK_CUR);
                fprintf(fin, "%c", 8);   /* 8 is ascii backspace dont know escape char */
                fprintf(fin, "%c", '\n');
               }
           }
    	
      fclose(fin);
      printf("Done.");
      }	
    }
    


Advertisement