You are not logged in.

1

Wednesday, July 14th 2010, 8:20pm

75+ chars file error

Hi,
I have very strange problem. I have this function:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
void MyClassOne::save(void) 
{
 QString temp, result ;
 temp = m_ui->lineEdit->text() ;
 int spaces = 0 ;
 if(temp.size()<75) spaces = (75-temp.size())/2 ;
 for(int i = 0 ; i < spaces ; i++) result+=' ' ;

     result += temp ; 
result += "\n" ;  
      pointerToMyClassTwo->saveToFile(result) ;
 }


And this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
void MyClassTwo::saveToFile(QString text)
 { 
 QString asterixes ; 
 for(int i = 0 ; i < 75 ; i++) 
asterixes+='*' ;
 asterixes+='\n' ; 
 const char * path = filePath.toStdString().c_str() ;
 int number = numberOfLines(path) ;
 if(number<=2) createFile(path, asterixes) ;

      addToFile(path, text) ;
 addToFile(path, asterixes) ;
 }



Functions: numberOfLines, createFile, addToFile are good for sure. They are using "fstream" library, but I think it is not important.
In code you can see '75' witch is something like line width in my file. When this width is 75 or less everything is working good. But when it's 76 or more it acts like those last two lines were missing. I must add that this number (75) isn't connected with anything it's just number of asterixes (****) witch are printed to file. When I changed asterix to other char for example '%' problem is still the same.

This post has been edited 1 times, last edit by "Lazarus" (Jul 14th 2010, 11:13pm)


Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Thursday, July 15th 2010, 2:02pm

Not sure what all is going on here even after reading it. However I think this might help -

In your save function, if you wanting to prepend or fill blanks up to the text of line edit contents than something like this might be more intuitive:

Source code

1
2
3
4
5
6
7
void MyClassOne::save()
{
  QString result;
  result.fill( ' ', lineEdit->maxLength() - lineEdit->size() );  // result = "       xxxx"  based on lineEdit->setMaxLength( 75 );
  result += lineEdit->text();
  pointerToMyClassTwo->saveToFile(result); 
}


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// void MyClassTwo::saveToFile(QString text) // change ->
void MyClassTwo::saveToFile( const QString &text )  // rem update .h
 { 
 QString asterixes; 
 fill.asterixes( '*', 75 );  // limit from lineEdit?

 // 
 const char * path = filePath.toStdString().c_str() ;  // ?? filePath
 int number = numberOfLines(path);  // ?? numberOfLines()
 if(number<=2) createFile(path, asterixes) ;  // ?? createFile() of stars or addFile()

  // ??
   addToFile(path, text) ;
  addToFile(path, asterixes) ;
}


What is the overall goal of the code above; to wrap a text in stars above and below
and set text right justified in middle, then try something like this:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
QString stars;
stars.fill( '*', 75 );

QFile file( "text.txt" ); // #include <QFile>
if( file.open( QIODevice::Append ) ){
  QTextStream textStream( &file ); // #include <QTextStream>
  textStream << '\n';
  textStream << stars << '\n';
  textStream << result << '\n'
  textStream << stars << '\n';
  file.close();
}

3

Friday, September 17th 2010, 8:07am

75+ chars file error

C file input/output abilities:

This is a continuation from previous Module. For C++ and MFC (Windows GUI programming) it is called Serialization and the topics are in Single Document Interface (SDI) and Multiple Document Interface (MDI). The C++ standard input/output file is discussed in C++ file input/output. The source code for this Module is: C file input/output program source codes. Trainee must be able to understand and use:

A sequential access file – Read and Write related functions.
Characters, lines and blocks disk file reading and writing related functions.
A Random access files – Read and Write related functions.
Some C File Management Functions.
Other C libraries used for file I/O.

9.6.2 Reading And Writing Disk File
  • The previous program example does not do anything with the tkk1103.txt text file, except open and close it. Some text has been saved in tkk1103.txt, so how can you read them from the file?
  • In C you can perform I/O operations in the following ways:

Quoted

  1. Read or write one character at a time.
  2. Read or write one line of text (that is, one line of characters) at a time.
  3. Read or write one block of characters at a time.
9.6.2.1 One Character At A Time
  • Among the C I/O functions, there is a pair of functions, fgetc()/fgetc_s() and fputc()/fputc_s(), that can be used to read from or write to a disk file one character at a time. The _s version is a secure version.
  • The prototype for the fgetc() function is:
int fgetc(FILE *stream);
  • The stream is the file pointer that is associated with a stream. The fgetc() function fetches the next character from the stream specified by stream. The function then returns the value of an int that is converted from the character.
  • The prototype for the fputc() function is:
int fputc(int c, FILE *stream);

  • c is an int value that represents a character. In fact, the int value is converted to an unsigned char before being output. stream is the file pointer that is associated with a stream. The fputc() function returns the character written if the function is successful, otherwise, it returns EOF. After a character is written, the fputc() function advances the associated file pointer.
  • Let explore the program example. Before that, you have to create two text files named, testone.txt and testtwo.txt then save it in the same folder where the your main() program is or provide the full path strings if the files is in another folder. Then for filetesttwo.txt, write the following texts and save it.
OPENING, READING, WRITING AND CLOSING FILE
-------------------------------------------
Testing file. This file named testtwo.txt.
After opening files for reading and writing,
without error, content of this file (testtwo.txt)
will be read and output (write) to the other
file named testone.txt and standard
output(screen/console) character by character!!!

---HAPPY LEARNING FOLKS!!!----

Content of file testtwo.txt
  • Then, if you run the program with no error, modify the content of the testtwo.txt, recompile and rerun the program. The displaying texts and the content of testone.txt also will change.
  • Next check also the content of testone.txt file, the content should be same as testtwo.txt file and the texts displayed on your screen.
1. // reading and writing one character at a time
2. #include <stdio.h>
3. #include <stdlib.h>
4.
5. // enumerated data type, SUCCESS = 0, FAIL = 1
6. enum {SUCCESS, FAIL};
7.
8. // prototype function for reading from and writing...
9. void CharReadWrite(FILE *fin, FILE *fout);
10.
11. int main()
12. {
13. // declare two file pointers...
14. FILE *fptr1, *fptr2;
15. // define the two files name...
16. char filename1[] = "testone.txt";
17. char filename2[] = "testtwo.txt";
18. int reval = SUCCESS;
19.
20. // test the opening filename1 for writing....
21. // if fails...
22. if ((fptr1 = fopen(filename1, "w")) == NULL)
23. {
24. printf("Problem, cannot open %s.\n", filename1);
25. reval = FAIL;
26. }
27. // if opening filename1 for writing is successful,
28. // test for opening for reading filename2, if fails...
29. else if ((fptr2 = fopen(filename2, "r")) == NULL)
30. {
31. printf("Problem, cannot open %s.\n", filename2);
32.
33. reval = FAIL;








_________________________________________________________




Want to get-on Google's first page and loads of traffic to your website? Hire a SEO Specialist from Ocean Groups http://oceangroups.org/]seo pecialist [/url]