Thread: Reading characters from string/file in blocks of 10

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Reading characters from string/file in blocks of 10

    Hi,

    Would just like to ask about reading characters from string/file.

    Say if I have a sequence of 200 letters, how do I get blocks of 10 letters each with the blocks shifting by one position to the right? Example:

    First block of 10 letters would be position 1-10 in the string/file.
    Second block of 10 letters would be position 2-11 in the string/file.
    Third block of 10 letters would be position 3-12 in the string/file and so on until I reach the last possible block.

    And once I get those blocks, how do I arrange them neatly in a column in excel format?

    Cheers,
    Z070111

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Hi,

    This is what I have done so far...

    Code:
    #include <stdio.h>
    
    main ()
    {
    	FILE *from, *in;
    	int i;
    	char ch[100];
    
    	if ((from = fopen("output2.txt", "r"))==NULL)
    		printf("Error opening output2.txt");
    	else
    		if((in = fopen("input2.xls", "w")) == NULL)
    		printf("Error opening input2.xls");
    		else
    		{
    			for(i = 10; i <= 40; i++)
    			{
    				while(fgets(ch, sizeof (i), from))
    				fputs(ch, in);
    			}
    		}
    		fclose(from);
    		fclose(in);
    }
    I need to create blocks of 10 char to 40 char. But it doesn't seem to do that for me but instead it prints out just the whole long sequence.

    Another thing is, I need to put these blocks of char into a column of an excel spreadsheet but I can't find much about editting in excel using C.

    Anyone can assist?

    Thanks much in advance,
    Z070111

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Doesn't Excel import comma separated values for it's text input, into columns?

    For code, I was thinking of something like this, which shows it on the screen.

    Code:
    #include <stdio.h>
    
    int main ()  {
      int i, j, done;
      char ch[] =  "abcdefghijklmnopqrstu";
    
      printf("\n\n\n\n\n\n");
     // Use fgets() and put the whole long string into your 
     //(much larger) char array, then:
      i = done = 0;
      while(!done)  {
        for(j = i; j < i+10; j++) {
          putchar(ch[j]);
          if(!ch[j+1]) {
            done = 1;
            break;
          }
        }
        if(!done)
          printf(", ");
        ++i;
        if(i % 7 == 6)
          printf("\n");
      } 
      printf("\n\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Thanks Adak!!!

    Your help is much appreciated. =)

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Z070111 View Post
    Thanks Adak!!!

    Your help is much appreciated. =)
    You're welcome. I thought that might brighten your day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault
    By Lost__Soul in forum C Programming
    Replies: 46
    Last Post: 04-28-2003, 04:24 AM
  2. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM
  3. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 11:00 AM
  4. Heaps...
    By Nutshell in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 08:54 AM
  5. Formatting Output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-26-2002, 01:33 AM